What is PHP?
PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web/Wap development and can be embedded into HTML/WML.
here is an exmple:
HTML Example:
Notice how this is different from a script written in other languages like Perl or C -- instead of writing a program with lots of commands to output HTML, you write an HTML script with some embedded code to do something (in this case, output some text). The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".
The best things in using PHP are that it is extremely simple for a newcomer, but offers many advanced features for a professional programmer. Don't be afraid reading the long list of PHP's features. You can jump in, in a short time, and start writing simple scripts in a few hours.
---------------
A simple tutotial: (Your First PHP Enabled page)
Here we would like to show the very basics of PHP in a short, simple tutorial. This text only deals with dynamic webpage creation with PHP, though PHP is not only capable of creating webpages.
PHP-enabled web pages are treated just like regular HTML/WML pages and you can create and edit them the same way you normally create regular HTML/WML pages.
Our first php script hello.php
HTML:
WML Example:
Note that this is not like a CGI script. The file does not need to be executable or special in any way. Think of it as a normal HTML/WML file which happens to have a set of special tags available to you that do a lot of interesting things. Wink
If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled. Ask your administrator to enable it for you.
The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML/WML file like this all you want.
Now that you have successfully created a working PHP script, it is time to create the most famous PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information.
-------
Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT'].
Note: $_SERVER is a special reserved PHP variable that contains all web server information. It is known as an autoglobal (or superglobal). These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS arrays instead, such as $HTTP_SERVER_VARS. Although deprecated, these older variables still exist.
To display this variable, you can simply do:
A sample output of this script may be:
Mozilla/4.0 (compatible;MSIE 5.01; Windows NT 5.0)
There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful.
$_SERVER is just one variable that PHP automatically makes available to you.
-------
Show all predefined variables with phpinfo()
When you load up this file in your browser, you will see a page full of information about PHP along with a list of all the variables available to you.
You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer you can do this:
Using Control Stuctures and functions:
A sample output of this script may be:
You are using Internet Explorer
Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up any introductory PHP book and read the first couple of chapters. You can find a list of php books at http://php.net/books.php
The second concept we introduced was the strpos() function call. 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. If it does not return FALSE, the if expression evaluates to TRUE and the code within its {braces} is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other functions.
---------
We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block:
Mixing HTML and PHP modes
Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on the result of strpos(). In other words, it depends on whether the string MSIE was found or not.
--------
Thats it for now, i will get more into detail on php functions and jumping in and out of php...
PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web/Wap development and can be embedded into HTML/WML.
here is an exmple:
HTML Example:
Code:
<html> <head> <title>Example</title> </head> <body> <?php echo "Hi, I'm a PHP script!"; ?> </body> </html>
The best things in using PHP are that it is extremely simple for a newcomer, but offers many advanced features for a professional programmer. Don't be afraid reading the long list of PHP's features. You can jump in, in a short time, and start writing simple scripts in a few hours.
---------------
A simple tutotial: (Your First PHP Enabled page)
Here we would like to show the very basics of PHP in a short, simple tutorial. This text only deals with dynamic webpage creation with PHP, though PHP is not only capable of creating webpages.
PHP-enabled web pages are treated just like regular HTML/WML pages and you can create and edit them the same way you normally create regular HTML/WML pages.
Our first php script hello.php
HTML:
Code:
<html> <head> <title>PHP Test</title> </head> <body> <?php echo ' Hello World I am a PHP script</p>'; ?> </body> </html>
Code:
<?php { header ('Content-Type: text/vnd.wap.wml'); echo '<xml version="1.0"?', '> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> <wml>'; } { echo ' <card id="main" title="phptest"> Hello World I am a PHP script</p> </card>'; } { echo ' </wml>'; } ?>
If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled. Ask your administrator to enable it for you.
The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML/WML file like this all you want.
Now that you have successfully created a working PHP script, it is time to create the most famous PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information.
-------
Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT'].
Note: $_SERVER is a special reserved PHP variable that contains all web server information. It is known as an autoglobal (or superglobal). These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS arrays instead, such as $HTTP_SERVER_VARS. Although deprecated, these older variables still exist.
To display this variable, you can simply do:
Code:
<?php echo $_SERVER['HTTP_USER_AGENT']; ?>
Mozilla/4.0 (compatible;MSIE 5.01; Windows NT 5.0)
There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful.
$_SERVER is just one variable that PHP automatically makes available to you.
-------
Show all predefined variables with phpinfo()
Code:
<?php phpinfo(); ?>
You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer you can do this:
Using Control Stuctures and functions:
Code:
<?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { echo 'You are using Internet Explorer '; } ?>
You are using Internet Explorer
Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up any introductory PHP book and read the first couple of chapters. You can find a list of php books at http://php.net/books.php
The second concept we introduced was the strpos() function call. 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. If it does not return FALSE, the if expression evaluates to TRUE and the code within its {braces} is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other functions.
---------
We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block:
Mixing HTML and PHP modes
Code:
<?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { ?> <h3>strpos must have returned non-false</h3> <center>[b]You are using Internet Explorer[/b]</center> <?php } else { ?> <h3>strpos must have returned false</h3> <center>[b]You are not using Internet Explorer[/b]</center> <?php } ?>
--------
Thats it for now, i will get more into detail on php functions and jumping in and out of php...
Comment