SlideShare a Scribd company logo
1 of 66
Intro to PHP
Ahmed Farag
Information Systems Dept.
Faculty of Computers and Information
Menoufia University, Egypt.
Intro
Intro to
PHP
ā€¢ PHP is a server scripting language, and a powerful tool for
making dynamic and interactive Web pages.
ā€¢ PHP is a widely-used, free, and efficient alternative to
competitors such as Microsoft's ASP.
ā€¢ PHP 7 is the latest stable release.
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 add, delete, modify data in your database
ā€¢ PHP can be used to control user-access
ā€¢ PHP can encrypt data
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 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 supports a wide range of databases
ā€¢ PHP is free. Download it from the official PHP resource:
www.php.net
ā€¢ PHP is easy to learn and runs efficiently on the server side
What Do
I Need?
ā€¢ To start using PHP, you can:
1. Find a web host with PHP and MySQL support
2. Install a web server on your own PC, and then install
PHP and MySQL (Xampp)
ā€¢ Set Up PHP on Your Own PC
ā€¢ install a web server
ā€¢ install PHP
ā€¢ install a database, such as MySQL
Basic PHP Syntax
Basic
PHP
Syntax
ā€¢ A PHP script is executed on the server, and the plain HTML
result is sent back to the browser.
ā€¢ A PHP script can be placed anywhere in the document.
ā€¢ 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
// PHP code goes here
?>
Example
ā€¢ Note: PHP statements end with a semicolon (;).
ā€¢ 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:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Commen
ts 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.
<!DOCTYPE html>
<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 multiple
lines
*/
// You can also use comments to leave out parts
of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
Basic
PHP
Syntax
ā€¢ PHP Case Sensitivity:
ā€¢ In PHP, NO keywords (e.g. if, else, while, echo, etc.),
classes, functions, and user-defined functions are case-
sensitive.
ā€¢ In the example below, all three echo statements below
are legal (and equal):
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Basic
PHP
Syntax
ā€¢ However; all variable names are case-sensitive.
ā€¢ In the example below, only the first statement will display the
value of the $color variable (this is because $color, $COLOR,
and $coLOR are treated as three different variables):
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
PHP 7 Variables
Variables
ā€¢ Variables are "containers" for storing information.
ā€¢ In PHP, a variable starts with the $ sign, followed by the name
of the variable:
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
Variables
ā€¢ Note: When you assign a text value to a variable, put quotes
around the value.
ā€¢ Note: Unlike other programming languages, PHP has no
command for declaring a variable. It is created the moment
you first assign a value to it.
ā€¢ Think of variables as containers for storing data.
Variables
ā€¢ 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:
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
PHP 7 Data Types
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
PHP
String
ā€¢ A string is a sequence of characters, like "Hello world!".
ā€¢ A string can be any text inside quotes. You can use single or
double quotes:
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP
Integer
ā€¢ An integer data type is a non-decimal number between -
2,147,483,648 and 2,147,483,647.
ā€¢ Rules for integers:
ā€¢ An integer must have at least one digit
ā€¢ An integer must not have a decimal point
ā€¢ An integer can be either positive or negative
ā€¢ Integers can be specified in three formats: decimal (10-
based), hexadecimal (16-based - prefixed with 0x) or
octal (8-based - prefixed with 0)
ā€¢ In the following example $x is an integer. The PHP var_dump()
function returns the data type and value:
<?php
$x = 5985;
var_dump($x);
?>
PHP Float
ā€¢ A float (floating point number) is a number with a decimal
point.
ā€¢ In the following example $x is a float. The PHP var_dump()
function returns the data type and value:
<?php
$x = 10.365;
var_dump($x);
?>
PHP
Boolean
ā€¢ A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
PHP
Array
ā€¢ An array stores multiple values in one single variable.
ā€¢ In the following example $cars is an array. The PHP
var_dump() function returns the data type and value:
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP
NULL
Value
ā€¢ A variable of data type NULL is a variable that has no value
assigned to it.
ā€¢ Tip: If a variable is created without a value, it is automatically
assigned a value of NULL.
ā€¢ Variables can also be emptied by setting the value to NULL:
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
PHP
NULL
Value
ā€¢ A variable of data type NULL is a variable that has no value
assigned to it.
ā€¢ Tip: If a variable is created without a value, it is automatically
assigned a value of NULL.
ā€¢ Variables can also be emptied by setting the value to NULL:
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
PHP 7 if...else...elseif Statements
if...else...
elseif
Statemen
ts
ā€¢ Conditional statements are used to perform different actions
based on different conditions.
ā€¢ In PHP we have the following conditional statements:
ā€¢ if statement - executes some code if one condition is true
ā€¢ if...else statement - executes some code if a condition is true
and another code if that condition is false
ā€¢ if...elseif...else statement - executes different codes for more
than two conditions
ā€¢ switch statement - selects one of many blocks of code to be
executed
if
ā€¢ PHP - The if Statement
ā€¢ The example below will output "Have a good day!" if the
current time (HOUR) is less than 20:
if (condition) {
code to be executed if condition is true;
}
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
if...else
ā€¢ The example below will output "Have a good day!" if the
current time is less than 20, and "Have a good night!"
otherwise:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
if...elseif..
.else
ā€¢ The example below will output "Have a good morning!" if the
current time is less than 10, and "Have a good day!" if the
current time is less than 20. Otherwise it will output "Have a
good night!":
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this
condition is true;
} else {
code to be executed if all conditions are false;
}
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
switch
ā€¢ The switch statement is used to perform different actions
based on different conditions.
ā€¢ Use the switch statement to select one of many blocks of
code to be executed.
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different
from all labels;
}
switch
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither
red, blue, nor green!";
}
?>
PHP 7 while Loops
PHP
Loops
ā€¢ Often when you write code, you want the same block of code
to run over and over again in a row. Instead of adding several
almost equal code-lines in a script, we can use loops to
perform a task like this.
ā€¢ 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
Loops
ā€¢ The example below first sets a variable $x to 1 ($x = 1). Then,
the while loop will continue to run as long as $x is less than, or
equal to 5 ($x <= 5). $x will increase by 1 each time the loop
runs ($x++):
while (condition is true) {
code to be executed;
}
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
PHP
Loops
ā€¢ The example below first sets a variable $x to 1 ($x = 1). Then,
the do while loop will write some output, and then increment
the variable $x with 1. Then the condition is checked (is $x
less than, or equal to 5?), and the loop will continue to run as
long as $x is less than, or equal to 5:
do {
code to be executed;
} while (condition is true);
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
PHP
Loops
ā€¢ Notice that in a do while loop the condition is tested AFTER
executing the statements within the loop. This means that the
do while loop would execute its statements at least once,
even if the condition is false the first time.
ā€¢ The example below sets the $x variable to 6, then it runs the
loop, and then the condition is checked:
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
PHP
Loops
ā€¢ Parameters:
ā€¢ init counter: Initialize the loop counter value
ā€¢ test counter: Evaluated for each loop iteration. If it evaluates to
TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
ā€¢ increment counter: Increases the loop counter value
ā€¢ The example below displays the numbers from 0 to 10:
for (init counter; test counter; increment
counter) {
code to be executed;
}
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
PHP
Loops
ā€¢ The foreach loop works only on arrays, and is used to loop
through each key/value pair in an array.
ā€¢ For every loop iteration, the value of the current array
element is assigned to $value and the array pointer is moved
by one, until it reaches the last array element.
ā€¢ The following example demonstrates a loop that will output
the values of the given array ($colors):
<?php
$colors
= array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
PHP 7 Arrays
PHP 7
Arrays
ā€¢ An array stores multiple values in one single variable:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . "
and " . $cars[2] . ".";
?>
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
ā€¢ In PHP, the array() function is used to create an array:
ā€¢ 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
array();
Indexed
Arrays
ā€¢ PHP Indexed Arrays
ā€¢ There are two ways to create indexed arrays:
ā€¢ The index can be assigned automatically (index always starts
at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
ā€¢ or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
ā€¢ The following example creates an indexed array named $cars,
assigns three elements to it, and then prints a text containing
the array values:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . "
and " . $cars[2] . ".";
?>
Indexed
Arrays
ā€¢ 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>";
}
?>
Associati
ve Arrays
ā€¢ PHP 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";
<?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Associati
ve Arrays
ā€¢ PHP Associative Arrays
<?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"
);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
PHP 7 Form Handling
Form
Handling
ā€¢ The PHP superglobals $_GET and $_POST are used to collect
form-data.
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
Form
Handling
ā€¢ The PHP superglobals $_GET and $_POST are used to collect
form-data.
ā€¢ When the user fills out the form above and clicks the submit
button, the form data is sent for processing to a PHP file
named "welcome.php". The form data is sent with the HTTP
POST method.
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
Form
Handling
ā€¢ To display the submitted data you could simply echo all the
variables. The "welcome.php" looks like this:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
The output could be something like this:
Welcome John
Your email address is john.doe@example.com
Form
Handling
ā€¢ The same result could also be achieved using the HTTP GET
method:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
and "welcome_get.php" looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
GET vs.
POST
ā€¢ Both GET and POST create an array (e.g. array( key1 =>
value1, key2 => value2, key3 => value3, ...)). This array holds
key/value pairs, where keys are the names of the form
controls and values are the input data from the user.
ā€¢ Both GET and POST are treated as $_GET and $_POST.
ā€¢ $_GET is an array of variables passed to the current script via
the URL parameters.
ā€¢ $_POST is an array of variables passed to the current script via
the HTTP POST method.
GET vs.
POST
ā€¢ When to use GET?
ā€¢ Information sent from a form with the GET method is visible
to everyone (all variable names and values are displayed in
the URL).
ā€¢ GET also has limits on the amount of information to send.
The limitation is about 2000 characters. However, because the
variables are displayed in the URL, it is possible to bookmark
the page. This can be useful in some cases.
ā€¢ GET may be used for sending non-sensitive data.
ā€¢ Note: GET should NEVER be used for sending passwords or
other sensitive information!
GET vs.
POST
ā€¢ When to use POST?
ā€¢ Information sent from a form with the POST method is
invisible to others (all names/values are embedded within
the body of the HTTP request) and has no limits on the
amount of information to send.
ā€¢ Moreover POST supports advanced functionality such as
support for multi-part binary input while uploading files to
server.
ā€¢ However, because the variables are not displayed in the URL,
it is not possible to bookmark the page.
PHP 7 MySQL Database
Connect
to
MySQL
ā€¢ PHP 5 and later can work with a MySQL database using:
1. MySQLi extension (the "i" stands for improved)
2. PDO (PHP Data Objects)
ā€¢ PDO will work on 12 different database systems, whereas
MySQLi will only work with MySQL databases.
ā€¢ So, if you have to switch your project to use another database,
PDO makes the process easy. You only have to change the
connection string and a few queries. With MySQLi, you will
need to rewrite the entire code - queries included.
ā€¢ Both support Prepared Statements. Prepared Statements
protect from SQL injection, and are very important for web
application security.
Connect
to
MySQL
ā€¢ Before we can access data in the MySQL database, we need to
be able to connect to the server:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
PHP 7
Insert
Data Into
MySQL
ā€¢ After a database and a table have been created, we can start
adding data in them.
ā€¢ Here are some syntax rules to follow:
ā€¢ The SQL query must be quoted in PHP
ā€¢ String values inside the SQL query must be quoted
ā€¢ Numeric values must not be quoted
ā€¢ The word NULL must not be quoted
PHP 7
Insert
Data Into
MySQL
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Select
Data
From a
MySQL
Database
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " .
$row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Delete
Data From
a MySQL
Table
Using
MySQLi
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Update
Data In a
MySQL
Table
Using
MySQLi
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

More Related Content

What's hot (20)

Php basics
Php basicsPhp basics
Php basics
Ā 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
Ā 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
Ā 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
Ā 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
Ā 
Php Unit 1
Php Unit 1Php Unit 1
Php Unit 1
Ā 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
Ā 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
Ā 
Dev traning 2016 basics of PHP
Dev traning 2016   basics of PHPDev traning 2016   basics of PHP
Dev traning 2016 basics of PHP
Ā 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
Ā 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Ā 
Introduction to php php++
Introduction to php php++Introduction to php php++
Introduction to php php++
Ā 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
Ā 
PHP - Introduction to PHP
PHP -  Introduction to PHPPHP -  Introduction to PHP
PHP - Introduction to PHP
Ā 
Php mysql
Php mysqlPhp mysql
Php mysql
Ā 
Php(report)
Php(report)Php(report)
Php(report)
Ā 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
Ā 
Basic PHP
Basic PHPBasic PHP
Basic PHP
Ā 
Php.ppt
Php.pptPhp.ppt
Php.ppt
Ā 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
Ā 

Similar to Intro to php

php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptxHambaAbebe2
Ā 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQLArti Parab Academics
Ā 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_masterjeeva indra
Ā 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...anshkhurana01
Ā 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSAminiel Michael
Ā 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web DevelopmentNiladri Karmakar
Ā 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaDeepak Rajput
Ā 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)Guni Sonow
Ā 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfShaimaaMohamedGalal
Ā 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit universityMandakini Kumari
Ā 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitdSorabh Jain
Ā 

Similar to Intro to php (20)

php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
Ā 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
Ā 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
Ā 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
Ā 
Php Basics
Php BasicsPhp Basics
Php Basics
Ā 
Php
PhpPhp
Php
Ā 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
Ā 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Ā 
Php unit i
Php unit iPhp unit i
Php unit i
Ā 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
Ā 
Php
PhpPhp
Php
Ā 
Php1
Php1Php1
Php1
Ā 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
Ā 
Basic php 5
Basic php 5Basic php 5
Basic php 5
Ā 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
Ā 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Ā 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)
Ā 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
Ā 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
Ā 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
Ā 

More from Ahmed Farag

MYSql manage db
MYSql manage dbMYSql manage db
MYSql manage dbAhmed Farag
Ā 
Normalization
NormalizationNormalization
NormalizationAhmed Farag
Ā 
Sql modifying data - MYSQL part I
Sql modifying data - MYSQL part ISql modifying data - MYSQL part I
Sql modifying data - MYSQL part IAhmed Farag
Ā 
MYSQL using set operators
MYSQL using set operatorsMYSQL using set operators
MYSQL using set operatorsAhmed Farag
Ā 
MYSQL join
MYSQL joinMYSQL join
MYSQL joinAhmed Farag
Ā 
MYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingMYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingAhmed Farag
Ā 
Introduction to NoSQL and MongoDB
Introduction to NoSQL and MongoDBIntroduction to NoSQL and MongoDB
Introduction to NoSQL and MongoDBAhmed Farag
Ā 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP conceptsAhmed Farag
Ā 
Introduction to object-oriented analysis and design (OOA/D)
Introduction to object-oriented analysis and design (OOA/D)Introduction to object-oriented analysis and design (OOA/D)
Introduction to object-oriented analysis and design (OOA/D)Ahmed Farag
Ā 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams Ahmed Farag
Ā 
Functions C++
Functions C++Functions C++
Functions C++Ahmed Farag
Ā 
intro to pointer C++
intro to  pointer C++intro to  pointer C++
intro to pointer C++Ahmed Farag
Ā 
Intro to C++
Intro to C++Intro to C++
Intro to C++Ahmed Farag
Ā 

More from Ahmed Farag (14)

MYSql manage db
MYSql manage dbMYSql manage db
MYSql manage db
Ā 
Normalization
NormalizationNormalization
Normalization
Ā 
Sql modifying data - MYSQL part I
Sql modifying data - MYSQL part ISql modifying data - MYSQL part I
Sql modifying data - MYSQL part I
Ā 
MYSQL using set operators
MYSQL using set operatorsMYSQL using set operators
MYSQL using set operators
Ā 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
Ā 
MYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingMYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-having
Ā 
Introduction to NoSQL and MongoDB
Introduction to NoSQL and MongoDBIntroduction to NoSQL and MongoDB
Introduction to NoSQL and MongoDB
Ā 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
Ā 
Introduction to object-oriented analysis and design (OOA/D)
Introduction to object-oriented analysis and design (OOA/D)Introduction to object-oriented analysis and design (OOA/D)
Introduction to object-oriented analysis and design (OOA/D)
Ā 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
Ā 
OOP C++
OOP C++OOP C++
OOP C++
Ā 
Functions C++
Functions C++Functions C++
Functions C++
Ā 
intro to pointer C++
intro to  pointer C++intro to  pointer C++
intro to pointer C++
Ā 
Intro to C++
Intro to C++Intro to C++
Intro to C++
Ā 

Recently uploaded

šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜RTylerCroy
Ā 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
Ā 
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...Alan Dix
Ā 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
Ā 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
Ā 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
Ā 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
Ā 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
Ā 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
Ā 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĆŗjo
Ā 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
Ā 
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
Ā 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
Ā 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
Ā 

Recently uploaded (20)

šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Ā 
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Ā 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Ā 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Ā 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Ā 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
Ā 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
Ā 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
Ā 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Ā 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
Ā 
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Ā 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Ā 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
Ā 

Intro to php

  • 1. Intro to PHP Ahmed Farag Information Systems Dept. Faculty of Computers and Information Menoufia University, Egypt.
  • 3. Intro to PHP ā€¢ PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. ā€¢ PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. ā€¢ PHP 7 is the latest stable release.
  • 4. 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
  • 5. 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"
  • 6. 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 add, delete, modify data in your database ā€¢ PHP can be used to control user-access ā€¢ PHP can encrypt data
  • 7. 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 add, delete, modify data in your database ā€¢ PHP can be used to control user-access ā€¢ PHP can encrypt data
  • 8. Why PHP? ā€¢ PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) ā€¢ PHP supports a wide range of databases ā€¢ PHP is free. Download it from the official PHP resource: www.php.net ā€¢ PHP is easy to learn and runs efficiently on the server side
  • 9. What Do I Need? ā€¢ To start using PHP, you can: 1. Find a web host with PHP and MySQL support 2. Install a web server on your own PC, and then install PHP and MySQL (Xampp) ā€¢ Set Up PHP on Your Own PC ā€¢ install a web server ā€¢ install PHP ā€¢ install a database, such as MySQL
  • 11. Basic PHP Syntax ā€¢ A PHP script is executed on the server, and the plain HTML result is sent back to the browser. ā€¢ A PHP script can be placed anywhere in the document. ā€¢ 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 // PHP code goes here ?>
  • 12. Example ā€¢ Note: PHP statements end with a semicolon (;). ā€¢ 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: <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
  • 13. Commen ts 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. <!DOCTYPE html> <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 multiple lines */ // You can also use comments to leave out parts of a code line $x = 5 /* + 15 */ + 5; echo $x; ?> </body> </html>
  • 14. Basic PHP Syntax ā€¢ PHP Case Sensitivity: ā€¢ In PHP, NO keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are case- sensitive. ā€¢ In the example below, all three echo statements below are legal (and equal): <!DOCTYPE html> <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>
  • 15. Basic PHP Syntax ā€¢ However; all variable names are case-sensitive. ā€¢ In the example below, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different variables): <!DOCTYPE html> <html> <body> <?php $color = "red"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?> </body> </html>
  • 17. Variables ā€¢ Variables are "containers" for storing information. ā€¢ In PHP, a variable starts with the $ sign, followed by the name of the variable: <html> <body> <?php $txt = "Hello world!"; $x = 5; $y = 10.5; echo $txt; echo "<br>"; echo $x; echo "<br>"; echo $y; ?> </body> </html>
  • 18. Variables ā€¢ Note: When you assign a text value to a variable, put quotes around the value. ā€¢ Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it. ā€¢ Think of variables as containers for storing data.
  • 19. Variables ā€¢ 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: <?php $txt = "W3Schools.com"; echo "I love $txt!"; ?> <?php $txt = "W3Schools.com"; echo "I love " . $txt . "!"; ?> <?php $x = 5; $y = 4; echo $x + $y; ?>
  • 20. PHP 7 Data Types
  • 21. 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
  • 22. PHP String ā€¢ A string is a sequence of characters, like "Hello world!". ā€¢ A string can be any text inside quotes. You can use single or double quotes: <?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?>
  • 23. PHP Integer ā€¢ An integer data type is a non-decimal number between - 2,147,483,648 and 2,147,483,647. ā€¢ Rules for integers: ā€¢ An integer must have at least one digit ā€¢ An integer must not have a decimal point ā€¢ An integer can be either positive or negative ā€¢ Integers can be specified in three formats: decimal (10- based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0) ā€¢ In the following example $x is an integer. The PHP var_dump() function returns the data type and value: <?php $x = 5985; var_dump($x); ?>
  • 24. PHP Float ā€¢ A float (floating point number) is a number with a decimal point. ā€¢ In the following example $x is a float. The PHP var_dump() function returns the data type and value: <?php $x = 10.365; var_dump($x); ?>
  • 25. PHP Boolean ā€¢ A Boolean represents two possible states: TRUE or FALSE. $x = true; $y = false;
  • 26. PHP Array ā€¢ An array stores multiple values in one single variable. ā€¢ In the following example $cars is an array. The PHP var_dump() function returns the data type and value: <?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?>
  • 27. PHP NULL Value ā€¢ A variable of data type NULL is a variable that has no value assigned to it. ā€¢ Tip: If a variable is created without a value, it is automatically assigned a value of NULL. ā€¢ Variables can also be emptied by setting the value to NULL: <?php $x = "Hello world!"; $x = null; var_dump($x); ?>
  • 28. PHP NULL Value ā€¢ A variable of data type NULL is a variable that has no value assigned to it. ā€¢ Tip: If a variable is created without a value, it is automatically assigned a value of NULL. ā€¢ Variables can also be emptied by setting the value to NULL: <?php $x = "Hello world!"; $x = null; var_dump($x); ?>
  • 30. if...else... elseif Statemen ts ā€¢ Conditional statements are used to perform different actions based on different conditions. ā€¢ In PHP we have the following conditional statements: ā€¢ if statement - executes some code if one condition is true ā€¢ if...else statement - executes some code if a condition is true and another code if that condition is false ā€¢ if...elseif...else statement - executes different codes for more than two conditions ā€¢ switch statement - selects one of many blocks of code to be executed
  • 31. if ā€¢ PHP - The if Statement ā€¢ The example below will output "Have a good day!" if the current time (HOUR) is less than 20: if (condition) { code to be executed if condition is true; } <?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } ?>
  • 32. if...else ā€¢ The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } <?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
  • 33. if...elseif.. .else ā€¢ The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!": if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if first condition is false and this condition is true; } else { code to be executed if all conditions are false; } <?php $t = date("H"); if ($t < "10") { echo "Have a good morning!"; } elseif ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
  • 34. switch ā€¢ The switch statement is used to perform different actions based on different conditions. ā€¢ Use the switch statement to select one of many blocks of code to be executed. switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
  • 35. switch <?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?>
  • 36. PHP 7 while Loops
  • 37. PHP Loops ā€¢ Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use loops to perform a task like this. ā€¢ 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
  • 38. PHP Loops ā€¢ The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++): while (condition is true) { code to be executed; } <?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?>
  • 39. PHP Loops ā€¢ The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5: do { code to be executed; } while (condition is true); <?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?>
  • 40. PHP Loops ā€¢ Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time. ā€¢ The example below sets the $x variable to 6, then it runs the loop, and then the condition is checked: <?php $x = 6; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?>
  • 41. PHP Loops ā€¢ Parameters: ā€¢ init counter: Initialize the loop counter value ā€¢ test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. ā€¢ increment counter: Increases the loop counter value ā€¢ The example below displays the numbers from 0 to 10: for (init counter; test counter; increment counter) { code to be executed; } <?php for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>"; } ?>
  • 42. PHP Loops ā€¢ The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. ā€¢ For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element. ā€¢ The following example demonstrates a loop that will output the values of the given array ($colors): <?php $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?>
  • 44. PHP 7 Arrays ā€¢ An array stores multiple values in one single variable: <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
  • 45. 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.
  • 46. Create an Array in PHP ā€¢ In PHP, the array() function is used to create an array: ā€¢ 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 array();
  • 47. Indexed Arrays ā€¢ PHP Indexed Arrays ā€¢ There are two ways to create indexed arrays: ā€¢ The index can be assigned automatically (index always starts at 0), like this: $cars = array("Volvo", "BMW", "Toyota"); ā€¢ or the index can be assigned manually: $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota"; ā€¢ The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values: <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
  • 48. Indexed Arrays ā€¢ 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>"; } ?>
  • 49. Associati ve Arrays ā€¢ PHP 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"; <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?>
  • 50. Associati ve Arrays ā€¢ PHP Associative Arrays <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43" ); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?>
  • 51. PHP 7 Form Handling
  • 52. Form Handling ā€¢ The PHP superglobals $_GET and $_POST are used to collect form-data. <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
  • 53. Form Handling ā€¢ The PHP superglobals $_GET and $_POST are used to collect form-data. ā€¢ When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
  • 54. Form Handling ā€¢ To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this: <html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html> The output could be something like this: Welcome John Your email address is john.doe@example.com
  • 55. Form Handling ā€¢ The same result could also be achieved using the HTTP GET method: <html> <body> <form action="welcome_get.php" method="get"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html> and "welcome_get.php" looks like this: <html> <body> Welcome <?php echo $_GET["name"]; ?><br> Your email address is: <?php echo $_GET["email"]; ?> </body> </html>
  • 56. GET vs. POST ā€¢ Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. ā€¢ Both GET and POST are treated as $_GET and $_POST. ā€¢ $_GET is an array of variables passed to the current script via the URL parameters. ā€¢ $_POST is an array of variables passed to the current script via the HTTP POST method.
  • 57. GET vs. POST ā€¢ When to use GET? ā€¢ Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). ā€¢ GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. ā€¢ GET may be used for sending non-sensitive data. ā€¢ Note: GET should NEVER be used for sending passwords or other sensitive information!
  • 58. GET vs. POST ā€¢ When to use POST? ā€¢ Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send. ā€¢ Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server. ā€¢ However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
  • 59. PHP 7 MySQL Database
  • 60. Connect to MySQL ā€¢ PHP 5 and later can work with a MySQL database using: 1. MySQLi extension (the "i" stands for improved) 2. PDO (PHP Data Objects) ā€¢ PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. ā€¢ So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included. ā€¢ Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security.
  • 61. Connect to MySQL ā€¢ Before we can access data in the MySQL database, we need to be able to connect to the server: <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>
  • 62. PHP 7 Insert Data Into MySQL ā€¢ After a database and a table have been created, we can start adding data in them. ā€¢ Here are some syntax rules to follow: ā€¢ The SQL query must be quoted in PHP ā€¢ String values inside the SQL query must be quoted ā€¢ Numeric values must not be quoted ā€¢ The word NULL must not be quoted
  • 63. PHP 7 Insert Data Into MySQL <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?>
  • 64. Select Data From a MySQL Database <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?>
  • 65. Delete Data From a MySQL Table Using MySQLi <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // sql to delete a record $sql = "DELETE FROM MyGuests WHERE id=3"; if (mysqli_query($conn, $sql)) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 66. Update Data In a MySQL Table Using MySQLi <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } mysqli_close($conn); ?>