Introduction To PHP: Prepared By: Fadi Sharif

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

INTRODUCTION TO PHP

1 Prepared By:
Fadi Sharif
SERVER SIDE TECHNIQUES
 Server side scripting requires installation on the server
side
 Typically client sees only html and it unaware that the
html was produced by a server side script
 Does not require any installations or add-ons on the
client

2
SERVER SIDE TECHNIQUES
 PHP
 Java EE: Servlet, JSP

 .NET

 CGI / Perl (Very old)

 Ruby

…

3
CLIENT SIDE TECHNIQUES
 Requires that the client supports the technique
 JavaScript, Applet, Flash, ActiveX…

4
INTRODUCTION TO PHP
 PHP stands for HyperText Preprocessor
 PHP is a computer scripting language. What does
Scripting language mean?
 Originally designed for producing dynamic web
pages
 Appeared in 1995
 PHP Group is responsible for the language, no
formal specification
 Free software
 Runs on most operating systems and platforms
 URL: http://www.php.net

5
INTRODUCTION TO PHP (cont..)
 PHP is a scripting Language: this means the code is
interpreted every time the code is executed
 This means that scripting languages are slower than
compiled languages which only requires the code to be
compiled once and the you can run the code without
compiling it every time.

6
WHY PHP?
 PHP runs on different platforms (Windows, Linux, Unix, etc.)
 PHP is compatible with almost all servers used today (Apache,
IIS, etc.)
 PHP is FREE to download from the official PHP resource: 
www.php.net
 PHP is easy to learn and runs efficiently on the server side

7
PHP INSTALLATION
 What do you need?
To create real dynamic web pages using PHP you
will need the following: ( it’s all free) 
 PHP
Download PHP for free here: http://www.php.net/downloads.php
 MySQL Database
Download MySQL for free here: 
http://www.mysql.com/downloads/
 Apache Web Server
Download Apache for free here: 
http://httpd.apache.org/download.cgi

8
PHP INSTALLATION
 How to do it the easy way? 
Instead of installing PHP, MySQL, Apache separately, you can
install it all as one package and have it all ready and
configured for you.
 To do so you need a XAMPP/LAMP package
 XAMPP stands for: Windows Apache MySql and
Apache
 LAMP, where L Stands for Linux, is designed for Linux
users.

9
PHP INSTALLATION
 To install the XAMP server go to
 Or for easier installation use XAMPP
http://www.apachefriends.org/en/xampp.html
 This package will install for you the following:
 PHP 8
 MySql
 Apache WebServer
 PHP my Admin
 And more…

10
THREE-TIERED WEB SITE: LAMP/XAMPP
Client example request Server
User-agent: Firefox GET / HTTP/1.1 Apache HTTP Server
Host: www.tamk.fi
User-Agent: Mozilla/5.0 (Mac..)
...

response PHP

Database
MySQL
11
RESPONSE

12
INTRODUCTION TO PHP SYNTAX
 PHP has quite easy syntax, if you are familiar
with any C-type language
 It has all the same structures that you are
familiar with other programming languages
 PHP is designed to output to browser, but it is
possible to do also CLI apps.

13
PHP - SPECIFICS
 Delimiters:<?php ?> or just <? ?>
 PHP parses code within delimiters

 Code outside delimiter sent to output, not parsed

 Block comments /* */

 Inline comments // #

14
PHP VS. C++
 Similarities:
 Syntax nearly the same (For/While/If)
 Requires semicolons after each statement ;
 Assignment is right to left ($num = 56;)
 Object-Oriented (Class support, inheritance, virtuals,
polymorphism)
 Functions!
 Types are nearly the same (booleans, integers, strings,
etc.)

15
PHP VERSUS C++
 Differences:
 Variables begin with $ sign ($name = “John Doe”;)
 No explicit declaration of variable types
 Introduction of “lazy” functions (foreach, explode,
mail)
 No Function Overloading
 “Hidden” functions-within-a-function
 Compiled/interpreted during every page load
 Documented!
 Echo for output
16
EXAMPLE
<?php
$name = “Mohammed”;
echo "Hello " . $name;
?>

17
VARIABLES
 Variables in PHP are represented by a dollar sign
 PHP supports eight types:
 boolean, integer, float, double, array, object, resource and
NULL

18
EXAMPLE (PHP.NET)
<?php
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer

echo gettype($a_bool); // prints out: boolean


echo gettype($a_str); // prints out: string

// If this is an integer, increment it by four


if (is_int($an_int)) {
$an_int += 4;
}

// If $bool is a string, print it out


// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_bool";
} 19
?>
PHP IS LOOSELY TYPED LANGUAGE
 In PHP, a variable does not need to be declared before
adding a value to it.
 In the example above, you see that you do 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 a strongly typed programming language, you have to
declare (define) the type and name of the variable before
using it.
 In PHP, the variable is declared automatically when you
use it.
20
NAMING VARIABLES
 Case-sensitivity
 Start with letter or _

 After that you can have numbers, letters and _


 $var = 'Bob';
 $Var = 'Joe';
 print "$var, $Var";     
 $4site = 'not yet';   
 $_4site = 'not yet';   

21
CONSTANTS
 You cannot alter the value of constant after declaration
 define(CONSTANT, "value");
 print CONSTANT;

22
STRING VARIABLE
 String variables are used for values that contain
characters.
 After we create a string we can manipulate it. A string
can be used directly in a function or it can be stored in a
variable.
 Below, the PHP script assigns the text "Hello World" to a
string variable called $txt:
Example:
<?php
$txt="Hello World";
echo $txt;
?>
23
 
THE CONCATENATION OPERATOR

 There is only one string operator in PHP.


 The concatenation operator (.)  is used to put two string values
together.
 To concatenate two string variables together, use the concatenation
operator:
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
 The output of the code above will be: Hello World! What a nice
day!
 If we look at the code above you see that we used the concatenation
operator two times. This is because we had to insert a third string (a
space character), to separate the two strings. 24
THE STRLEN() FUNCTION

 The strlen() function is used to return the length of a


string.
 Let's find the length of a string:

<?php
echo strlen("Hello world!");
?>
 The output of the code above will be: 12

 The length of a string is often used in loops or other


functions, when it is important to know when the string
ends. (i.e. in a loop, we would want to stop the loop after
the last character in the string).
25
THE STRPOS() FUNCTION

 The strpos() function is used to search for a character/text within a


string.
 If a match is found, this function will return the character position of
the first match. If no match is found, it will return FALSE.
 Let's see if we can find the string "world" in our string:
 <?php
echo strpos("Hello world!","world");
?>
 The output of the code above will be: 6
 The position of the string "world" in the example above is 6. The
reason that it is 6 (and not 7), is that the first character position in
the string is 0, and not 1.
 For a complete reference for String functions see:
http://www.php.net/manual/en/ref.strings.php 26
PHP OPERATORS
 Arithmetic Operators

27
PHP OPERATORS
 Assignment Operators

28
PHP OPERATORS
 Incrementing/ Decrementing Operators
 Dsfdsfdsfds

29
PHP OPERATORS
 Comparison Operators

30
PHP OPERATORS
 Logical Operators

31
CONTROL STRUCTURES
 If, else, elseif, switch
 while, do-while, for

 foreach

 break, continue

32
IF
<?php
if ($a > $b) {
   echo "a is bigger than b";
} else {
   echo "a is NOT bigger than b";
}
if ($a > $b) {
   echo "a is bigger than b";
} elseif ($a == $b) {
   echo "a is equal to b";
} else {
   echo "a is smaller than b";
}
?> 33
WHILE AND DO-WHILE
<?php
$a=0;
while($a<10){
print $a; $a++;
}

$i = 0;
do {
   print $i;
} while ($i > 0);
?> 34
FOR

for ($i = 1; $i <= 10; $i++) {


   print $i;
}

35
FOR LOOP EXAMPLE
<html>
<head>
</head>
<body>
<?php

for($i=1;$i<=6;$i++){
echo " <br><h" . $i . "> Hello world </h" . $i . ">" ;
}
$name = "ahmed”;
echo "welcome " . $name;
?>
</body>
</html> 36
THE HTML OUTPUT
<html>
<head>
</head>
<body>

<br><h1> Hello world </h1> <br><h2> Hello world </h2> <br><h3>


Hello world </h3> <br><h4> Hello world </h4> <br><h5> Hello
world </h5> <br><h6> Hello world </h6>

</body>
</html>
37
FOREACH
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
   echo $value;
}

38
SWITCH
switch ($i) {
case 0:
   echo "i equals 0";
   break;
case 1:
   echo "i equals 1";
   break;
case 2:
   echo "i equals 2";
   break;
} 39
PHP ARRAYS
In PHP, there are three kind of arrays:
 Numeric array - An array with a numeric index
 Associative array - An array where each ID key is associated with
a value
 Multidimensional array - An array containing one or more arrays

40
NUMERIC ARRAYS
Numeric arrays stores each array element with a numeric index
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the
index starts at 0):
$cars=array("Saab","Volvo","BMW","Toyota");
2. In the following example we assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";

41
NUMERIC ARRAYS
Example:
In the following example you access the variable values by
referring to the array name and index:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota"; 
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
 The code above will output: Saab and Volvo are Swedish
cars.
42
ASSOCIATIVE ARRAYS
 An associative array, each ID key is associated with a value.
 When storing data about specific named values, a numerical array is not
always the best way to do it.
 With associative arrays we can use the values as keys and assign values
to them.
 Example 1

In this example we use an array to assign ages to the different persons:


$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
 Example 2

This example is the same as example 1, but shows a different way of


creating the array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
 
43
ASSOCIATIVE ARRAYS
 Example 3
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";


?>
 The code above will output: Peter is 32 years old.

44
REFERERNCES
 www.w3schools.com
 www.php.net

45

You might also like