PHP Basic(二)Simple hello world

本文介绍了PHP的基础知识,包括如何运行PHP代码、PHP与HTML的交互方式、PHP的主要用途及应用场景等。此外,还提供了简单的PHP代码示例,展示了如何处理表单数据。
PHP Basic(二)Simple hello world

note of web site
http://www.php.net/manual/en/

Getting Started
introduction
here is a "server-php >> html >> browser" process illustration.

before html runs to show a webpage,php code runs first on web server.
<table>
<tr>
<td>
<?php
echo "php runs first!";
?>
</td>
</tr>
</table>
the first step is to run php code, we get:
<table>
<tr>
<td>
php runs first
</td>
</tr>
</table>

then, code is sent to browser, and we see something~

what is PHP?
The PHP code is enclosed in special start and end processing instructions <?php and ?> that allow you to jump into and out of "PHP mode".

What distinguishes PHP from something like client-side JavaScript is that the code is executed on the server, generating HTML which is then sent to the client.

what can PHP do?
PHP can do anything any other CGI program can do, such as collect form data, generate dynamic page content, or send and receive cookies. PHP can do much more.

There are three main areas where PHP scripts are used.
1. Server-side scripting.
This is the most traditional and main target field for PHP. You need three things to make this work.
The PHP parser (CGI or server module)
a web server
a web browser

All these can run on your home machine if you are just experimenting with PHP programming.

2. Command line scripting.

3. Writing desktop applications.
PHP-GTK is an extension to PHP, not available in the main distribution.

PHP supports a wide range of databases. Additionally PHP supports ODBC, the Open Database Connection standard, so you can connect to any other database supporting this world standard.

At last but not least, PHP has many other interesting extensions, the mnoGoSearch search engine functions, the IRC Gateway functions, many compression utilities(gzip,bz2,zip)...

A simple tutorial
<?php echo '<p>Hello</p>'; ?> php tag <?php ?>
<?php phpinfo(); ?> phpinfo get system information

Something Useful
We check the user agent string the browser sends as part of the HTTP request.
The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT']

$_SERVER is a special reserved PHP variable that contains all web server information.

if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
echo 'You are using Internet Explorer.<br />';
}

strpos() function
strpos() is a function built into PHP which searches a string for another string. In this case we are looking for 'MSIE' (so-called needle) inside $_SERVER['HTTP_USER_AGENT'] (so-called haystack). If the needle is found inside the haystack, the function returns the position of the needle relative to the start of the haystack. Otherwise, it returns FALSE.

Dealing with Forms
form.php:
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>

There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action.php page is called.

action.php:
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

htmlspecialchars() makes sure any characters that are special in html are properly encoded so people can't inject HTML tags or Javascript into your page.

The $_POST['name'] and $_POST['age'] variables are automatically set for you by PHP

If we used the method GET then our form information would live in the $_GET superglobal instead. You may also use the $_REQUEST superglobal, if you do not care about the source of your request data.

PHP languange references
http://www.php.net/manual/en/langref.php
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值