About Php
Before you continue: |
you should have a basic understanding of the following: HTML CSS JavaScript |
PHP is an acronym for |
"PHP: Hypertext Preprocessor" |
PHP is |
a widely-used, open source scripting language |
PHP is |
|
Php |
PHP is a server scripting language |
PHP files have extension |
".php" |
PHP files can contain |
text, HTML, CSS, JavaScript, and PHP code |
Basic PHP Syntax
A PHP script starts with <?php and ends with ?>: |
<?php // PHP code goes here ?> |
A PHP file normally contains HTML tags, and some PHP scripting code. |
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
|
Comments in PHP |
// single-line comment |
Basic PHP Syntax
A PHP script starts with <?php and ends with ?>: |
<?php // PHP code goes here ?> |
A PHP file normally contains HTML tags, and some PHP scripting code. |
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
|
Comments in PHP |
// or # are the single-line comment And /* multiple-lines comment block */ |
PHP Case Sensitivity: |
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.However; all variable names are case-sensitive. |
Creating (Declaring) PHP Variables |
variable starts with the $ sign, $x = 5; |
Output Variables |
echo statement: echo $x |
PHP is a Loosely Typed Language |
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. |
|
|
The Point 1
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: static $x = 0; |
PHP 5 Data Types
PHP Data Types |
String Integer Float (floating point numbers - also called double) Boolean Array Object NULL Resource |
PHP Array |
An array stores multiple values in one single variable.$cars = array("Volvo","BMW","Toyota"); |
PHP Object |
is a data type which stores data and information on how to process that data. |
PHP NULL Value |
Null is a special data type which can have only one value: NULL.$x = null; |
PHP Resource |
A common example of using the resource data type is a database call. |
PHP 5 Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
To create a constant, use the define() function.
Syntax
define(name, value, case-insensitive)
<?php
define("GREETING", "Welcome to Amozeh.com!");
echo GREETING;
?> |
PHP 5 Operators
=== Identical |
$x === $y Returns true if $x is equal to $y, and they are of the same type |
!== Not identical |
Returns true if $x is not equal to $y, or they are not of the same type |
PHP String Operators |
PHP has two operators that are specially designed for strings. |
. Concatenation |
$txt1 . $txt2 Concatenation of $txt1 and $txt2 |
.= Concatenation assignment |
$txt1 .= $txt2 Appends $txt2 to $txt1 |
PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
|
|
PHP Conditional Statements
The if Statement |
if ($t < "20") { echo "Have a good day!";
|
The if...else Statement |
if ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
|
The PHP switch Statement |
switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; ... default: echo "Your favorite color isn't exists!!"; }
|
PHP Loops
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
`<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?> ` |
PHP 5 Functions
The real power of PHP comes from its functions; it has more than 1000 built-in functions.
Besides the built-in PHP functions, we can create our own functions.
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?> |
PHP 5 Arrays
An array stores multiple values in one single variable: |
$cars = array("Volvo", "BMW", "Toyota"); |
In PHP, there are three types of arrays: |
Indexed arrays - Arrays with a numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays |
Indexed Arrays |
$cars = array("Volvo", "BMW", "Toyota");
OR $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota";
|
The count() function is used to return the length (the number of elements) of an array: |
<?php $cars = array("Volvo", "BMW", "Toyota"); echo count($cars); ?>
|
Loop Through an Indexed Array |
<?php $cars = array("Volvo", "BMW", "Toyota"); $arrlength = count($cars); for($x = 0; $x < $arrlength; $x++) { echo $cars[$x]; echo "<br>"; } ?>
|
Associative Arrays |
Associative arrays are arrays that use named keys that you assign to them. |
There are two ways to create an associative array: |
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
OR $age['Peter'] = "35"; $age['Ben'] = "37"; $age['Joe'] = "43";
|
Loop Through an Associative Array |
<?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?>
|
Multidimensional arrays will be explained in the PHP advanced section.
|