All-about-php-5
All-about-php-5
JONALINE Z. EUSTAQUIO
WHAT IS PHP?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code are executed on the server, and the result is returned to the browser as plain
HTML
PHP files have extension ".php"
WHAT CAN PHP DO?
What Do I Need?
To start using PHP, you can:
Find a web host with PHP and MySQL support
Install a web server on your own PC, and then install PHP and MySQL
USE A WEB HOST WITH PHP SUPPORT
If your server has activated support for PHP you do not need to do anything.
Just create some .php files, place them in your web directory, and the server will
automatically parse them for you.
You do not need to compile anything or install any extra tools.
Because PHP is free, most web hosts offer PHP support.
SET UP PHP ON YOUR OWN PC
A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP
function "echo" to output the text "Hello World!" on a web page:
Note: PHP statements end with a semicolon (;).
COMMENTS IN PHP
A comment in PHP code is a line that is not read/executed as part of the program. Its only
purpose is to be read by someone who is looking at the code.
Comments can be used to:
Let others understand what you are doing
Remind yourself of what you did - Most programmers have experienced coming back to
their own work a year or two later and having to re-figure out what they did. Comments
can remind you of what you were thinking when you wrote the code
PHP supports several ways of commenting:
Example
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
Run example »
PHP CASE SENSITIVITY
In PHP, NO keywords (e.g. if, else, while, echo, However; all variable names are case-sensitive.
etc.), classes, functions, and user-defined In the example below, only the first statement will display
functions are case-sensitive. the value of the $color variable (this is because $color,
In the example below, all three echo statements $COLOR, and $coLOR are treated as three different
below are legal (and equal): variables):
Example
Example
<!DOCTYPE html>
<!DOCTYPE html>
<html> <html>
<body> <body>
<?php <?php
ECHO "Hello World!<br>"; $color = "red";
echo "Hello World!<br>"; echo "My car is " . $color . "<br>";
EcHo "Hello World!<br>"; echo "My house is " . $COLOR . "<br>";
?> echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html> </body>
</html>
PHP 5 VARIABLES
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and
_)
Variable names are case-sensitive ($age and $AGE are two different variables)
Remember that PHP variable names are case-sensitive!
OUTPUT VARIABLES
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
Example
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
The following example will produce the same output as the example above:
Example
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
Run example »
The following example will output the sum of two variables:
Example
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
Run example »
Note: You will learn more about the echo statement and how to output data to the screen in the next chapter.
PHP IS A LOOSELY TYPED LANGUAGE
In the example above, notice that we did not have to tell PHP which data type the variable
is.
PHP automatically converts the variable to the correct data type, depending on its value.
In other languages such as C, C++, and Java, the programmer must declare the name and
type of the variable before using it.
PHP VARIABLES SCOPE
A variable declared outside a function has a A variable declared within a function has a
GLOBAL SCOPE and can only be accessed LOCAL SCOPE and can only be accessed within
outside a function: that function:
Example Example
<?php <?php
$x = 5; // global scope function myTest() {
$x = 5; // local scope
function myTest() { echo "<p>Variable x inside function is:
// using x inside this function will generate $x</p>";
an error }
echo "<p>Variable x inside function is: myTest();
$x</p>";
} // using x outside the function will generate an
myTest(); error
echo "<p>Variable x outside function is:
echo "<p>Variable x outside function is:
$x</p>";
$x</p>";
?> ?>
PHP THE GLOBAL KEYWORD
The global keyword is used to access a global variable from PHP also stores all global variables in an array called
within a function. $GLOBALS[index]. The index holds the name of the
To do this, use the global keyword before the variables variable. This array is also accessible from within
(inside the function): functions and can be used to update global variables
directly.
Example The example above can be rewritten like this:
<?php
Example
$x = 5; <?php
$y = 10; $x = 5;
$y = 10;
function myTest() {
global $x, $y; function myTest() {
$y = $x + $y; $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
} }
myTest();
myTest(); echo $y; // outputs 15
echo $y; // outputs 15 ?>
?>
PHP THE STATIC KEYWORD
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local
variable NOT to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable:
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
Then, each time the function is called, that variable will still have the information it contained from the last time the
function was called.
Note: The variable is still local to the function.
WHAT IS AN ARRAY?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars and find a specific one? And what if
you had not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access the values by
referring to an index number.
CREATE AN ARRAY IN PHP