Learn PHP by Examples
In this tutorial, I will walk through the most popular examples to learn PhP. The topics includes simple php example, php get and post methods, php arrays, and php object.
PhP hello word
|
1 2 3 4 5 6 7 8 |
<html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html> |
Here the echo keyword is used to print out the html string on your browser.
Get system information from PHP
<?php phpinfo(); ?>|
1 2 3 |
<?php echo $_SERVER['HTTP_USER_AGENT']; ?> |
Mozilla/4.0 (compatible; MSIE 6.0; Linux Ubuntu..)
PHP html form example
|
1 2 3 4 5 6 7 8 |
<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> Hi <?php echo htmlspecialchars($_POST['name']); ?>. You are <?php echo (int)$_POST['age']; ?> years old. |
PhP Get vs Post
PHP $_POST Variable
An associative array of variables passed to the current script via the HTTP POST method when usingapplication/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
This is a ‘superglobal’, or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.
PHP $_GET Variable
An associative array of variables passed to the current script via the URL parameters.
PhP multiple selected forms:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php print <<<_HTML_ <html> <body> <form method="post" action="value.php"> <select name="flower[ ]" multiple> <option value="flower">FLOWER</option> <option value="rose">ROSE</option> <option value="lilly">LILLY</option> <option value="jasmine">JASMINE</option> <option value="lotus">LOTUS</option> <option value="tulips">TULIPS</option> </select> <input type="submit" name="submit" value=Submit> </form> </body> </html> _HTML_ ?> <?php foreach ($_POST['flower'] as $names) { print "You are selected $names<br/>"; } ?> |
Determining variable types
PHP includes several functions which find out what type a variable is, such as: gettype(), is_array(), is_float(), is_int(), is_object(), and is_string().
PHP arrays
PHP arrays is the most important data structures. I have written another post about php array examples. Please refer to that post.
PHP object
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php class foo { function do_foo() { echo "Doing foo."; } } $bar = new foo; $bar->do_foo(); ?> |











