Introduction to PHP
What is PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
What is a PHP File?
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?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP resource: [Link]
PHP is easy to learn and runs efficiently on the server side
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
?>
EX PHP:
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Comments in PHP:
< html>
< body>
<?php
// This is a single line comment
# This is also a single line comment
/*
This is a multiple lines comment block
that spans over more than
one line
*/
?>
< /body>
< /html>
PHP Case Sensitivity:
In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are NOT case-
sensitive.
In the example below, all three echo statements below are legal (and equal):
Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
However; in PHP, all variables are case-sensitive.
PHP Variables
Creating (Declaring) PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
PHP 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)
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
local
global
static
Local Scope:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
PHP The global Keyword
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
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:
<?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.
PHP Constants
Syntax
define(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is
false
Example:
<html>
<body>
<?php
define("PI","3.14");
define("GREETING", "Welcome to [Link]!",false);
echo "<h1>". PI ."</h2><br>";
echo "<h1>". greeting ."</h2><br>";
?>
</body>
</html>
PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Example:
<html>
<body>
<?php
$x=10; //integer type
$y=25.675;//float type
$z='hi how are you';//string type
$m=false;//boolean type
$n=null;//null type
echo $x."<br>";
echo $y."<br>";
echo $z."<br>";
echo $m."<br>";
echo $n;
?>
</body>
</html>
The PHP echo Statement
echo is a language construct, and can be used with or without parentheses: echo or echo().
Ex1:
<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple arameters.";
?>
The PHP print Statement
print is also a language construct, and can be used with or without parentheses: print or print().
Ex1:
<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
PHP Arithmetic Operators
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y Show it
»
- Subtraction $x - $y Difference of $x and $y Show it
»
* Multiplication $x * $y Product of $x and $y Show it
»
/ Division $x / $y Quotient of $x and $y Show it
»
% Modulus $x % $y Remainder of $x divided by $y Show it
»
** Exponentiation $x ** $y Result of raising $x to the $y'th power
(Introduced in PHP 5.6)
PHP Assignment Operators
Assignment Same as... Description
x=y x=y The left operand gets set to the value of the Show it »
expression on the right
x += y x=x+y Addition Show it »
x -= y x=x-y Subtraction Show it »
x *= y x=x*y Multiplication Show it »
x /= y x=x/y Division Show it »
x %= y x=x%y Modulus
PHP Comparison Operators
Operator Name Example Result Show it
== Equal $x == $y Returns true if $x is equal to $y Show it
»
=== Identical $x === Returns true if $x is equal to $y, and they Show it
$y are of the same type »
!= Not equal $x != $y Returns true if $x is not equal to $y Show it
»
<> Not equal $x <> $y Returns true if $x is not equal to $y Show it
»
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they Show it
are not of the same type »
> Greater than $x > $y Returns true if $x is greater than $y Show it
»
< Less than $x < $y Returns true if $x is less than $y Show it
»
>= Greater than or $x >= $y Returns true if $x is greater than or equal Show it
equal to to $y »
<= Less than or $x <= $y Returns true if $x is less than or equal to $y
equal to
PHP Increment / Decrement Operators
Operator Name Description
++$x Pre-increment Increments $x by one, then returns $x Show it »
$x++ Post-increment Returns $x, then increments $x by one Show it »
--$x Pre-decrement Decrements $x by one, then returns $x Show it »
$x-- Post-decrement Returns $x, then decrements $x by one
PHP Logical Operators
Operator Name Example Result Show it
and And $x and $y True if both $x and $y are true Show it »
or Or $x or $y True if either $x or $y is true Show it »
xor Xor $x xor $y True if either $x or $y is true, but Show it »
not both
&& And $x && $y True if both $x and $y are true Show it »
|| Or $x || $y True if either $x or $y is true Show it »
! Not !$x True if $x is not true
PHP String Operators
Operator Name Example Result
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and Show it
$txt2 »
.= Concatenation $txt1 .= $txt2 Appends $txt2 to $txt1 Show it
assignment »
PHP Array Operators
Operator Name Example Result Show it
+ Union $x + $y Union of $x and $y Show it
»
== Equality $x == $y Returns true if $x and $y have the same Show it
key/value pairs »
=== Identity $x === $y Returns true if $x and $y have the same Show it
key/value pairs in the same order and of the »
same types
!= Inequality $x != $y Returns true if $x is not equal to $y Show it
»
<> Inequality $x <> $y Returns true if $x is not equal to $y Show it
»
!== Non-identity $x !== $y Returns true if $x is not identical to $y
Array In PHP:
<html>
<body>
<?php
$cars=array("volvo","audi",benz);
echo $cars[0];
echo $cars[1];
echo $cars[2];
?>
</body>
PHP Type Casting:
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$val = "test";
$val2 = "10";
$intval = (int)$val; // 0
echo var_dump($intval)."<br>";
$intval2 = (int)$val2; // 10
echo var_dump($intval2)."<br>";
$boolval = (bool)$intval; // false
echo var_dump($boolval)."<br>";
$boolval2 = (bool)$intval2; // true
echo var_dump($boolval2)."<br>";
$var3=10;
$var4=(string)$var3;
echo var_dump($var4)."<br>";
?>
</body>
</html>