- PHP stands for PHP: Hypertext Preprocessor
- PHP is a server-side scripting language
- PHP scripts are executed on the server
- PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
- PHP is an open source software
- PHP is free to download and use
Basic PHP Syntax
- A PHP script starts with <?php and ends with ?>
- The default file extension for PHP files is ".php"
- A PHP file normally contains HTML tags, and some PHP scripting code
- PHP statements are terminated by semicolon (;)
- In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are not case-sensitive
Hello World example
<html>
<body>
<?php
//
Use echo to print on console
echo “Hello World!”;
?>
</body>
</html>
Go to htdocs folder which is present in the
apache2triad installed folder. There create a folder and save this program with
.php extension such as Hello.php.
To execute hello world program, type in the address bar as follows:
http://localhost/MyPHPProgram/hello.php
Error Management
1. Compile-time errors:
To execute hello world program, type in the address bar as follows:
http://localhost/MyPHPProgram/hello.php
Error Management
1. Compile-time errors:
- Compile-time errors are detected by the parser while it is compiling a script.
- The compile-time errors cannot be trapped from within the script itself
- Fatal errors are the errors that halt the execution of a script.
- The fatal errors cannot be trapped.
- Recoverable errors that represent significant failures, but can still be handled in a safe way.
- Warnings are recoverable errors that indicate a run-time fault.
- Warnings do not halt the execution of the script.
- Notices indicate that an error condition occurred, but is not necessarily significant.
- Notices do not halt the execution of the script.
Finding errors present in the program
<html>
<body>
<?php
echo “Hello World!”;
//
here ? is missing
>
</body>
</html>
To find the
errors present in the program go to:
Start -> All
programs -> Apache2triad -> Apache2TriadCP
Then click on “PHP Error log”
The list of errors in the program is displayed along with the line number where the error has occurred.
Comments in PHP
- // Single line comment (C++ and Java-style comment)
- # Single line comment (Shell-style comments)
- /* Multiple line comment (C-style comments) */
PHP is a Loosely Typed Language
- In PHP, a variable does not need to be declared before adding a value to it
- PHP automatically converts the variable to the correct data type, depending on its value
- In PHP, the variable is declared automatically when you use it
- PHP variables must begin with a “$” sign
- Variables are used for storing values, like text strings, numbers or arrays
- The correct way of declaring a variable in PHP:
PHP Variables Example
<html>
<body>
<?php
$a = 25; // Numerical variable
$b = “Hello”; //
String variable
$c = 5.7; // Float variable
echo “Number is :
”.$a.“<br/>”;
echo “String is :
”.$b.“<br/>”;
echo “Float value : ”.$c;
?>
</body>
<html>
OUTPUT of the
above given Example is as follows:
Number is : 25
String is : Hello
Float value : 5.7
Global and locally-scoped variables
- Global variables can be used anywhere
- Local variables restricted to a function or class
<html>
<body>
<?php
$x=24; // global scope
// Function definition
function myFunction() {
$y=59; // local scope
echo
"Variable x is: $x <br>";
echo "Variable y is: $y";
}
myFunction();// Function call
echo
"Variable x is: $x";
echo
"<br>";
echo
"Variable y is: $y";
?>
</body>
</html>
OUTPUT of the
above given Example is as follows:
Variable x is:
Variable y is:
59
Test variables
outside the function:
Variable x is:
24
Variable y is:
Variable y is:
Static Keyword in PHP
- Static keyword is used when you first declare the variable
- Each time the function is called, that variable will still have the information it contained from the last time the function was called
Static Keyword Example
<html>
<body>
<?php
// Function definition
function
myFunction() {
static
$x=45;
echo
$x;
echo
"<br/>";
$x++;
}
//
Function call
myFunction();
myFunction();
myFunction();
myFunction();
myFunction();
?>
<body>
<html>
OUTPUT of the
above given Example is as follows:
45
46
47
48
49
46
47
48
49
Arithmetic Operators
Arithmetic operators allow performing basic mathematical operations
Arithmetic Operators Example
<html>
<body>
<?php
// Add 20, 10 and sum is stored in $i
$i=(20
+ 10);
// Subtract $i, 5 and difference is
stored in $j
$j=($i
- 5);
// Multiply $j, 4 and result is
stored in $k
$k=($j
* 4);
// Divide $k, 2 and result is stored
in $l
$l=($k
/ 2);
// Devide $l, 5 and remainder is
stored in $m
$m=($l
% 5);
echo
"i = ".$i."<br/>";
echo
"j = ".$j."<br/>";
echo "k =
".$k."<br/>";
echo
"l = ".$l."<br/>";
echo
"m = ".$m."<br/>";
?>
</body>
</html>
OUTPUT of the
above given Example is as follows:
i = 30
j = 25
k = 100
l = 50
m = 0
j = 25
k = 100
l = 50
m = 0
- Increment and Decrement Operators
Increment and Decrement Operators Example
<html><body><?php$i=10;$j=20;$i++;$j++;echo $i."<br/>";echo $j."<br/>";// Post increment$k=$i++;// Pre increment$l=++$j;echo $k."<br/>";echo $l;?></body></html>OUTPUT of the above given Example is as follows:11
21
11
22
Assignment Operators in PHP
Assignment operator is used to write a value to a variable
Assignment Operators Example
- <html><body><?php$a=5;echo "a=".$a;echo "<br/>";$b=10;$b += 20;echo "b=".$b;echo "<br/>";$c=15;$c -= 5;echo "c=".$c;echo "<br/>";$d=20;$d *= 2;echo "d=".$d;echo "<br/>";$e=25;$e /= 5;echo "e=".$e;echo "<br/>";$f=30;$f %= 4;echo "f=".$f;?></body></html>OUTPUT of the above given Example is as follows:a=5
b=30
c=10
d=40
e=5
f=2