0% found this document useful (0 votes)
93 views18 pages

Complete Guide: by Kaleem Abbas Jara

This document provides an overview and introduction to PHP. It discusses what PHP is, how PHP files work, and how to install PHP. It also introduces MySQL and explains that PHP supports dynamic scripting and databases like MySQL. The document is a table of contents that outlines topics for a complete PHP guide, including PHP syntax, variables, operators, conditional statements, arrays, loops, and functions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views18 pages

Complete Guide: by Kaleem Abbas Jara

This document provides an overview and introduction to PHP. It discusses what PHP is, how PHP files work, and how to install PHP. It also introduces MySQL and explains that PHP supports dynamic scripting and databases like MySQL. The document is a table of contents that outlines topics for a complete PHP guide, including PHP syntax, variables, operators, conditional statements, arrays, loops, and functions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

Complete PHP 1 CONTENTS

Complete guide

PHP
By
Kaleem Abbas Jara
Complete PHP 2 CONTENTS

CONTENTS
CONTENTS...................................................................................................................................................................2
PHP.................................................................................................................................................................................3
ABOUT PHP................................................................................................................................................................3
PHP FILE.....................................................................................................................................................................3
ABOUT MYSQL..........................................................................................................................................................3
PHP INSTALLATION....................................................................................................................................................3
What do you Need?...............................................................................................................................................4
PHP BASIC SYNTAX..................................................................................................................................................4
BASIC SYNTAX............................................................................................................................................................4
COMMENTS IN PHP....................................................................................................................................................4
VARIABLES IN PHP...................................................................................................................................................5
VARIABLE...................................................................................................................................................................5
Variable declaration.............................................................................................................................................5
Naming Rules for Variables..................................................................................................................................5
String Variables in PHP.......................................................................................................................................5
The Concatenation Operator................................................................................................................................6
The strlen() function..............................................................................................................................................6
The strpos() function.............................................................................................................................................6
PHP OPERATORS.......................................................................................................................................................7
PHP OPERATORS........................................................................................................................................................7
Arithmetic Operators............................................................................................................................................7
Assignment Operators...........................................................................................................................................7
Comparison Operators.........................................................................................................................................7
Logical Operators.................................................................................................................................................8
PHP IF...ELSE STATEMENTS..................................................................................................................................8
CONDITIONAL STATEMENTS.......................................................................................................................................8
The if Statement.....................................................................................................................................................8
The if...else Statement...........................................................................................................................................8
The if...elseif....else Statement...............................................................................................................................9
The PHP Switch Statement...................................................................................................................................9
PHP ARRAYS.............................................................................................................................................................10
WHAT IS AN ARRAY?...............................................................................................................................................10
Numeric Arrays...................................................................................................................................................10
Associative Arrays..............................................................................................................................................10
Multidimensional Arrays....................................................................................................................................11
PHP LOOPS................................................................................................................................................................12
WHAT IS LOOP?........................................................................................................................................................12
The while Loop....................................................................................................................................................12
The do...while Statement.....................................................................................................................................12
The for Loop........................................................................................................................................................13
The foreach Loop................................................................................................................................................14
PHP FUNCTIONS......................................................................................................................................................15
Complete PHP 3 CONTENTS

BUILT-FUNCTIONS....................................................................................................................................................15
Create a Function...............................................................................................................................................15
PHP Functions - Adding parameters..................................................................................................................15
PHP Functions - Return values...........................................................................................................................16

PHP
PHP is a more powerful tool for developing dynamic and interactive Web pages. It is the widely-
used and efficient alternative to the giant products like Microsoft's ASP in the present day
market.
In this PHP Book we will learn about PHP, and the way it executes scripts the server. But before
we proceed with PHP what we need to already know is the basic understanding of the following:
 HTML/XHTML
 JavaScript

About PHP
PHP stands for Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP supports databases like (MySQL, ODBC, Informix, Solid, PostgreSQL, Generic, Oracle,
Sybase, etc.)
PHP runs on different platforms (Windows, Linux, Unix, etc.)
PHP is compatible with all servers (Apache, IIS, etc.)

PHP file
A PHP file can be created using simple text editor
A PHP file can have text, HTML tags and scripts
A PHP file is returned to the browser as simple HTML 
A PHP file has extension of ".php", ".php3", or ".phtml"

About MySQL
MySQL is a database server ideal for small and large applications
MySQL is standard SQL compliant
MySQL possess the ability to compile on a number of platforms
PHP + MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

PHP Installation
In order to access the server with PHP support, you need to install following on your server:
Apache (or IIS), PHP, MySQL
Complete PHP 4 CONTENTS

What do you Need?


If server already supports PHP don't do anything. Create .php files in web directory, and server
will parse them. Most web hosts offer PHP support. However, if your server does not support
PHP, you must install PHP.

Here is a link to a PHP.net on how to install PHP5:


http://www.php.net/manual/en/install.php
Download MySQL
http://www.mysql.com/downloads/
Download Apache Server
http://httpd.apache.org/download.cgi

PHP Basic Syntax

Basic syntax
PHP scripting block starts with <?php and ends with ?> which can be placed anywhere in the
document. Servers with shorthand support PHP scripting block can be started with <? and end
with ?>.
<?php
?>
PHP file normally has HTML tags and some PHP scripting code.
Example
<html>
<body>

<?php
echo "I am new to PHP";
?>

</body>
</html>
Code line in PHP ends with a semicolon. Semicolon works as separator and distinguishes one set
of instructions from another. In PHP there are two statements to output text: echo and print.
Important note: The file must be saved with .php extension.

Comments in PHP
We use // to make a single-line comment or /* and */ to make a large comment block.
<html>
<body>

<?php
//Single line comment

/*
Complete PHP 5 CONTENTS

Large
block
comment
*/
?>

</body>
</html>

Variables in PHP

Variable
A variable is used for storing value text string, numbers or array. Variable can be used over and
over again after its declaration in script. In PHP, variables start with a $ sign symbol.
The way of declaring a variable in PHP is:
$var_name = value;
Example
<?php
$txt="Hey, I am new to PHP.";
$x=16;
?>

Variable declaration
A variable does not need to be declared before adding a value to it and also you do not have to
tell PHP which data type the variable is. PHP converts the variable to the correct data type
automatically. A variable is declared automatically when you use it.

Naming Rules for Variables


A variable name must start with a letter or an underscore "_"
A variable name can contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
A variable name should not contain spaces.
A string variable is used to store and manipulate text.

String Variables in PHP


String variables are used for characters data type. A string can be used in a function or it can be
stored in a variable.
Example
<?php
$txt="I am new to php";
echo $txt;
?>
Complete PHP 6 CONTENTS

Output
Hello World

The Concatenation Operator


The concatenation operator (.)  is used to combine two string values together.
Example
<?php
$txt1="I am new to";
$txt2="PHP";
echo $txt1 . " " . $txt2;
?>
Output
Hello World! What a nice day!

The strlen() function


The strlen() function returns the length of a string.
Example
<?php
echo strlen("I am new to PHP");
?>
Output
16

The strpos() function


The strpos() function searches for character within a string. On finding a match, function returns
the position of the first match. If no match is found, it will return FALSE.
Example
<?php
echo strpos("I am new to PHP","new");
?>
The output of the code above will be:
5
String position of "new” is position 5 not 6 because the first position in the string is 0, and not 1.
Complete PHP 7 CONTENTS

PHP Operators

PHP Operators
Following operators are used in PHP.

Arithmetic Operators
Operator Description Example Result
+ Addition x=2, x+2 4
- Subtraction x=2, 5-x 3
* Multiplication x=4, x*5 20
/ Division 15/5, 5/2 3
2.5
% Modulus (division remainder) 5%2, 1
10%8,10%2 2
0
++ Increment x=5, x++ x=6
-- Decrement x=5, x-- x=4

Assignment Operators
Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
Complete PHP 8 CONTENTS

%= x%=y x=x%y

Comparison Operators
Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
<> is not equal 5<>8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Logical Operators
Operator Description Example
&& and x=6, y=3 , (x < 10 && y > 1) returns true
|| or x=6, y=3 , (x==5 || y==5) returns false
! not x=6, y=3, !(x==y) returns true

PHP If...Else Statements


Conditional statements are used to perform different actions based on different conditions.

Conditional Statements
You can use conditional statements to perform different actions for different decisions.
In PHP we have the following conditional statements:

The if Statement
Use if statement to execute some code if a specified condition is true.
Example
<html>
<body>

<?php
$d=date("D");
if ($d=="Sun") echo "We are off today.";
?>

</body>
</html>

The if...else Statement


Use if....else statement to execute one code if condition is true and other if condition is false.
Example
<html>
Complete PHP 9 CONTENTS

<body>

<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>

</body>
</html>
Example
<html>
<body>

<?php
$d=date("D");
if ($d=="Fri")
  {
  echo "Hello!<br />";
  echo "Have a nice weekend!";
  echo "See you on Monday!";
  }
?>

</body>
</html>

The if...elseif....else Statement


Use if....elseif...else statement to make a selection from several blocks of code to be executed.
Example
<html>
<body>

<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Sun")
  echo "Have a nice Sunday!";
else
  echo "Have a nice day!";
?>

</body>
</html>
Complete PHP 10 CONTENTS

The PHP Switch Statement


Use the switch statement to select one of many blocks of code to be executed.
Example
<html>
<body>

<?php
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>

</body>
</html>

PHP Arrays

What is an Array?
Array is used to store multiple values in single variable. A variable stores only one value. Array
is a special variable, which can store multiple values in one single variable. 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

Numeric Arrays
A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In this example the index are automatically assigned (starting from 0):
$cars=array("Saab","Volvo","BMW","Toyota");
2. In this example index is manually assigned:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
Complete PHP 11 CONTENTS

$cars[3]="Toyota";
Example
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
Output
Saab and Volvo are Swedish cars.

Associative Arrays
An associative array, each ID key is associated with a value. With associative arrays we can use
the values as keys and assign values to them.
Example
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Example
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
The ID keys can be used in a script:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
Output
Peter is 32 years old.

Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each
element in the sub-array can be an array, and so on.
Example
$families = array
  (
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
Complete PHP 12 CONTENTS

  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );
Output
Array
(
[Griffin] => Array
  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
  )
[Quagmire] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)
Example
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
Output
Is Megan a part of the Griffin family?

PHP Loops

What is Loop?
Instead of same set of code again and again we use looping technique to run a block of code over
and over again. In PHP, we have the following looping statements:
while - loops through a block of code while a specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as a 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
Complete PHP 13 CONTENTS

The while Loop


The while loop executes a block of code while a condition is true.
Example
<html>
<body>

<?php
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }
?>

</body>
</html>
Output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

The do...while Statement


The do...while statement will always execute the block of code once, it will then check the
condition, and repeat the loop while the condition is true.
Example
<html>
<body>

<?php
$i=1;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<=5);
?>

</body>
</html>
Output:
The number is 2
The number is 3
The number is 4
Complete PHP 14 CONTENTS

The number is 5
The number is 6

The for Loop


The for loop is used when you know in advance how many times the script should run.
Syntax
for (init; condition; increment)
 {
  code to be executed;
 }
Parameters:
init: Used to set a counter
condition: If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment: Mostly used to increment a counter
Example
<html>
<body>

<?php
for ($i=1; $i<=5; $i++)
 {
  echo "The number is " . $i . "<br />";
 }
?>

</body>
</html>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

The foreach Loop


The foreach loop is used to loop through arrays.
Syntax
foreach ($array as $value)
 {
  code to be executed;
 }
For every loop iteration, the value of the current array element is assigned to $value (and the
array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array
value.
Example
<html>
<body>
Complete PHP 15 CONTENTS

<?php
$x=array("one","two","three");
foreach ($x as $value)
 {
  echo $value . "<br />";
 }
?>

</body>
</html>
Output:
one
two
three

PHP Functions

Built-Functions
Complete PHP 16 CONTENTS

In PHP, there are more than 700 built-in functions. To keep the script from being executed when
the page loads, you can put it into a function. A function is executed when it is called. Function
can be called a function from anywhere within a page.

Create a Function
A function will be executed by a call.
Syntax
function functionName()
{
code to be executed;
}
The function name can start with a letter or underscore (not a number)
Example
<html>
<body>

<?php
function writeName()
{
echo "Kai Jim Refsnes";
}

echo "My name is ";


writeName();
?>

</body>
</html>
Output:
My name is Kai Jim Refsnes

PHP Functions - Adding parameters


We can add parameters to add more functionality to a function, we. A parameter is just like a
variable. Parameters are specified after the function name, inside the parentheses.
Example 1
<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}

echo "My name is ";


writeName("Kai Jim");
echo "My sister's name is ";
Complete PHP 17 CONTENTS

writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>

</body>
</html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
Example 2
<html>
<body>

<?php
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}

echo "My name is ";


writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("Ståle","?");
?>

</body>
</html>
Output:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?

PHP Functions - Return values


To let a function return a value, use the return statement.
Example
<html>
<body>

<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
Complete PHP 18 CONTENTS

echo "1 + 16 = " . add(1,16);


?>

</body>
</html>
Output:
1 + 16 = 17

You might also like