0% found this document useful (0 votes)
29 views8 pages

PHP Basics

PHP is a server-side scripting language used for web development. It allows developers to add dynamic content to websites. PHP scripts can contain HTML code, text, and scripts. PHP files are executed on the server and returned to the browser as plain HTML. The document discusses PHP installation, syntax, variables, arrays, control structures like if/else statements and loops, and functions.

Uploaded by

hakeem3398
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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

PHP Basics

PHP is a server-side scripting language used for web development. It allows developers to add dynamic content to websites. PHP scripts can contain HTML code, text, and scripts. PHP files are executed on the server and returned to the browser as plain HTML. The document discusses PHP installation, syntax, variables, arrays, control structures like if/else statements and loops, and functions.

Uploaded by

hakeem3398
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 PDF, TXT or read online on Scribd
You are on page 1/ 8

6/28/2010

What is PHP?

z PHP stands for PHP: Hypertext Preprocessor


z PHP is a server
server-side
side scripting language, like JSP
z PHP scripts are executed on the server
z PHP is an open source software
z PHP is free to download and use
z PHP files can contain text, HTML tags and scripts
z PHP files are returned to the browser as plain HTML
z PHP files have a file extension of ".php", ".php3", or ".phtml"

6/28/2010 Hassan Khan, PUCIT 1

Why PHP?
z PHP runs on different platforms (Windows, Linux, Unix, etc.)
z PHP is compatible with almost all servers used today (Apache, IIS, etc.)
z PHP is FREE to download from the official PHP resource: www.php.net

z PHP is easy to learn and runs efficiently on the server side

6/28/2010 Hassan Khan, PUCIT 2

1
6/28/2010

PHP Installation
z Download PHP
Download PHP for free here: http://www.php.net/downloads.php
z Download MySQL Database
Download MySQL for free here: http://www.mysql.com/downloads/index.html
z Download Apache Server
Download Apache for free here: http://httpd.apache.org/download.cgi

6/28/2010 Hassan Khan, PUCIT 3

Configuration of PHP

Install
sta WAMP o on window
do oor LAMP o
on Linux
u pplatform
at o a and
d you do
don’tt need
eed a
any
y
configuration.

OR

For configuration please see the attached document.

6/28/2010 Hassan Khan, PUCIT 4

2
6/28/2010

PHP Syntax
z <?php
--------
--------
?>

z <html><body>
<?php
echo "Hello World";
?>
</body></html>

6/28/2010 Hassan Khan, PUCIT 5

Comments in PHP

z <html><body>

<?php
? h

//This is a comment

/*
This is
a comment
block
*/

?>

</body></html>

6/28/2010 Hassan Khan, PUCIT 6

3
6/28/2010

PHP Variables
z All variables in PHP start with a $ sign symbol.
$var_name = value;

<?php

$txt="Hello World!";
$x=16;

?>
z PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
You do not have to tell PHP which data type the variable is.
In PHP, the variable is declared automatically when you use it.
6/28/2010 Hassan Khan, PUCIT 7

PHP String Variables


z The Concatenation Operator
<?php
$txt1=" Hello"; $txt2=" Good Morning!";
echo $txt1 . " " . $txt2;
?> Output: Hello Good Morning!
z The strlen() function
<?php
echo strlen("Hello world!");
?> Output:12

z The strpos() function


<?php
echo strpos("Hello world!","world");
?> Output: 6

6/28/2010 Hassan Khan, PUCIT 8

4
6/28/2010

PHP If...Else Statements


z <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>

6/28/2010 Hassan Khan, PUCIT 9

PHP Arrays
In PHP, there are three kind of arrays:

z Numeric array
y - An arrayy with a numeric index

z Associative array - An array where each ID key is associated with a value

z Multidimensional array - An array containing one or more arrays

6/28/2010 Hassan Khan, PUCIT 10

5
6/28/2010

Numeric Arrays
z A numeric array 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";

6/28/2010 Hassan Khan, PUCIT 11

Associative Arrays
z An associative array, each ID key is associated with a value.
There are two methods to create a numeric array.

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

z $ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

6/28/2010 Hassan Khan, PUCIT 12

6
6/28/2010

Multidimensional Arrays
z $families = array z Array
( (
"Griffin"=>array [Griffin] => Array
( (
"Peter", [[0]] => Peter
"Lois"
Lois , [1] => Lois
"Megan" [2] => Megan
), )
"Quagmire"=>array [Quagmire] => Array
( (
"Glenn" [0] => Glenn
), )
"Brown"=>array [Brown] => Array
( (
"Cleveland", [0] => Cleveland
"Loretta", [[1]] => Loretta
"Junior"
Junior [2] => Junior
) )
); )

$families['Griffin'][2] = ?
6/28/2010 Hassan Khan, PUCIT 13

PHP Looping
z The while Loop
while($i<=5)
{ echo "The number is " . $i . "<br />“; $i++; }

z The do...while Statement


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

z The For Loop

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

z The foreach Loop (The foreach loop is used to loop through arrays.)

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

6/28/2010 Hassan Khan, PUCIT 14

7
6/28/2010

PHP Functions
z A function will be executed by a call to the function.
z You may call a function from anywhere within a page.

y
<html><body>

<?php

function add($x,$y)
{
$total=$x+$y;
return $total;
}

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

?>

</body></html>
6/28/2010 Hassan Khan, PUCIT 15

You might also like