PHP Basic(二)Simple hello world
PHP Basic(二)Simple hello worldnote 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
页:
[1]