Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
66 views40 pages

Introduction To PHP

PHP is a server-side scripting language used for web development. It allows developers to add dynamic content to websites. PHP scripts are executed on the server and generate HTML that is sent to the browser. PHP supports many databases and runs on platforms like Windows, Linux, and Unix. It is free to download and use.

Uploaded by

Jayaprasanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views40 pages

Introduction To PHP

PHP is a server-side scripting language used for web development. It allows developers to add dynamic content to websites. PHP scripts are executed on the server and generate HTML that is sent to the browser. PHP supports many databases and runs on platforms like Windows, Linux, and Unix. It is free to download and use.

Uploaded by

Jayaprasanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

1

INTRODUCTION TO PHP
2

ORIGINS AND USES OF PHP


• Origins
– Rasmus Lerdorf – 1994
– Developed to allow him to track visitors to his Web site
• PHP is an open-source product
• PHP is an acronym for Personal Home Page, or PHP:
Hypertext Preprocessor
• PHP is used for form handling, file processing, and
database access
3

OVERVIEW OF PHP
• PHP is a server-side scripting language, like ASP
• PHP scripts are executed on the server
• PHP supports many databases (MySQL, Informix, Oracle, Sybase,
Solid, PostgreSQL, Generic ODBC, etc.)
• PHP is an open source software (OSS)
• PHP is free to download and use
• What is a PHP File?
– PHP files may contain text, HTML tags and scripts
– PHP files are returned to the browser as plain HTML
– PHP files have a file extension of ".php", ".php3", or ".phtml"
• 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
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 send and receive cookies
• PHP can add, delete, modify data in your
database
• PHP can be used to control user-access
• PHP can encrypt data
How PHP works ?
• PHP is a server-side language that means the code
written in PHP resides on a host computer called a server.
The server sends Web pages to the requesting visitors
(you, the client, with your web browser)
PHP Client-Server
Client Server

1 URL 2

HTML 3 Script request

Visitor goes to a web site written in PHP HTML


1
PHP

2 Server reads the PHP and then processes it


according to its scripted directions.

3 PHP code tells the server to send the


appropriate data – HTML code. Treats it as a
standard HTML.
What you will need?

•A web server application (Apache, Xitami, or IIS)


•PHP
•My SQL
•Web Browser
•Text editor, PHP-capable WYSIWYG application (Adobe
Dreamweaver/Kompozer/Amaya) or IDE (integrated
development environment)
•FTP application if using remote server
PHP Installation
• You need to install web server before you
install PHP. There are many different web
servers available to choose from, so you
need to choose which one you prefer.
Two of the more popular web servers are:
• Apache
• Internet Information Services (IIS)
PHP Syntax
The PHP syntax is based on C, Java, and Perl,
Creating a PHP file is similar to creating an HTML
file. In fact, most PHP files are a mixture of PHP
code and HTML.

To create a PHP file, simply do the following:


1. Create a new file in your favorite editor
2. Type some PHP code
3. Save the file with a .php extension
Basic PHP Syntax
• A PHP script can be placed anywhere in the document.
• A PHP script starts with <?php and ends with ?>:
• <?php
// PHP code goes here
?>
• The default file extension for PHP files is ".php".
• A PHP file normally contains HTML tags, and some
PHP scripting code.
Basic Code Syntax
3 different forms declaring php
<html> <?
<head> PHP Code In Here
<title>PHP Syntax Example</title> ?>
</head>
<?php
<body> PHP Code In Here
<?php php?>
echo "PHP is easy!";
?> <script language="php">
</body> PHP Code In Here
</script>
</html>
Comments in PHP

PHP support three kinds of comment tags :


1. //
This is a one line comment
2. #
This is a Unix shell-style comment. It's also a one line comment
3. /* ..... */
Use this multi line comment if you need to.
Comments in PHP
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
XAMPP
• XAMPP is an open-source web server solution package. It is mainly used for web application
testing on a local host webserver.
To create a PHP file, simply do the following:
1. Create a new file in your favorite editor
2. Type some PHP code
3. Save the file with a .php extension

C:\xampp1\htdocs\myphp\helo.php
http://localhost/myphp/helo.php
Variables
•Variables are used for storing values, like text strings,
numbers or arrays.
•A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
•The correct way of declaring a variable in PHP:
$var_name = value;
String Variables in PHP

String variables are used for values that


contains characters.
<?php
$txt="Hello World";
echo $txt;
?>
PHP Variables
• In PHP, variable names must start with
a dollar sign ($). For example:
<?php
$myVariable = "PHP is easy!";
echo $myVariable;
?>
Variables name
Rules for PHP variables:
• A variable starts with the $ sign, followed by the
name of the variable
• A variable name must start with a letter or the
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and $AGE
are two different variables)
PHP Variables
<?php
$variable1 = 2;
$variable2 = 9;
$variable3 = $variable1 + $variable2;
echo $variable3;
?>
PHP Variables-Concatenation
<?php
$variable1 = 2;
$variable2 = 9;
$variable3 = $variable1 + $variable2;
echo $variable1 . " + " . $variable2 . " = "
. $variable3;
?>
22

PRIMITIVES, OPERATIONS, EXPRESSIONS


• Variables
– There are no type declarations
– An unassigned (unbound) variable has the value, NULL
• The unset function sets a variable to NULL
• The IsSet function is used to determine whether a variable is NULL
- error_reporting(15); - prevents PHP from using unbound
variables
• PHP has many predefined variables, including the
environment variables of the host operating system
– You can get a list of the predefined variables by calling phpinfo() in
a script
• There are eight primitive types:
– Four scalar types: Boolean, integer, double, and string
– Two compound types: array and object
– Two special types: resource and NULL
23

PRIMITIVES, OPERATIONS, EXPRESSIONS


• Integer & double are typical
• Strings
– Characters are single bytes
– String literals use single or double quotes
• Single-quoted string literals
– Embedded variables are NOT interpolated
– Embedded escape sequences are NOT recognized
• Double-quoted string literals
– Embedded variables ARE interpolated
• If there is a variable name in a double-quoted string but you don’t want it
interpolated, it must be back slashed
– Embedded escape sequences ARE recognized
• For both single- and double-quoted literal strings, embedded
delimiters must be back slashed
24

PRIMITIVES, OPERATIONS, EXPRESSIONS


• Boolean - values are true and false (case insensitive)
– 0 and "" and "0" are false; others are true
• Arithmetic Operators and Expressions
– Usual operators
• If the result of integer division is not an integer, a double is returned
• Any integer operation that results in overflow produces a double
• The modulus operator coerces its operands to integer, if necessary
• When a double is rounded to an integer, the rounding is always towards
zero
• Arithmetic functions
– floor, ceil, round, abs, min, max, rand, etc.
• String Operations and Functions
– The only operator is period, for catenation
– Indexing - $str{3} is the fourth character
25

PRIMITIVES, OPERATIONS, EXPRESSIONS


• String Operations and Functions (continued)
– Functions:
• strlen, strcmp, strpos, substr, as in C
• chop – remove whitespace from the right end
• trim – remove whitespace from both ends
• ltrim – remove whitespace from the left end
• strtolower, strtoupper
• Scalar Type Conversions
– String to numeric
• If the string contains an e or an E, it is converted to double; otherwise to
int
• If the string does not begin with a sign or a digit, zero is used
– Explicit conversions – casts
• e.g., (int)$total or intval($total) or settype($total,
"integer")
26

PRIMITIVES, OPERATIONS, EXPRESSIONS


• The type of a variable can be determined with gettype or
is_type
– gettype($total) - it may return "unknown“
– is_integer($total) – a predicate function
27

Output Variables
• Output from a PHP script is HTML that is sent to the
browser
• HTML is sent to the browser through standard output
• There are three ways to produce output: echo, print, and
printf
echo “Test“,”None”; # More than one parameter acceptable
echo("first <br />", $sum) # More than one, so ILLEGAL!
print "Welcome to my site!"; # Only one
printf – same like C
• PHP code is placed in the body of an HTML document
28

OUTPUTS
• An Example:
<html>
<head><title> Trivial php example </title>
</head>
<body>
<?php
print "Welcome to my Web site!";
?>
</body>
</html>
29

CONTROL STATEMENTS
• Control Expressions
– Relational operators - same as JavaScript, (including === and !==)
– Boolean operators - same as Perl (two sets, && and and, etc.)
• Selection statements
– if, if-else, elseif
– switch - as in C
• The switch expression type must be integer,double, or string
– while - just like C
– do-while - just like C
– for - just like C
– foreach - discussed later
– break - in any for, foreach, while, do-while, or switch
– continue - in any loop
30

CONTROL STATEMENTS
• HTML can be intermingled with PHP script

<?php
$a = 7;
$b = 7;
if ($a == $b) {
$a = 3 * $a;
?>
<br /> At this point, $a and $b are
equal <br />
So, we change $a to three times $a
<?php
}
?>
31

ARRAYS
• Not like the arrays of any other programming language
• A PHP array is a generalization of the arrays of other
languages
– A PHP array is really a mapping of keys to values,where the keys
can be numbers (to get a traditional array) or strings (to get a hash)
• Array creation
– Use the array() construct, which takes one or more key => value
pairs as parameters and returns an array of them
– The keys are non-negative integer literals or string literals
– The values can be anything
$list = array(0 => "apples", 1 => "oranges", 2 =>
"grapes")
• This is a “regular” array of strings
32

ARRAYS
• If a key is omitted and there have been integer keys, the default key will
be the largest current key + 1
– If a key is omitted and there have been no integer keys, 0 is the default key
– If a key appears that has already appeared, the new value will overwrite the
old one
• Arrays can have mixed kinds of elements
$list = array("make" => "Cessna","model" =>
"C210","year" => 1960, 3 => "sold");
$list = array(1, 3, 5, 7, 9);
$list = array(5, 3 => 7, 5 => 10, "month" => "May");
$colors = array('red', 'blue', 'green', 'yellow');
33

ARRAYS
• Accessing array elements – use brackets
$list[4] = 7;
$list["day"] = "Tuesday";
$list[] = 17;
– If an element with the specified key does not exist, it is created
– If the array does not exist, the array is created
• The keys or values can be extracted from an array
$highs = array("Mon" => 74, "Tue" => 70,"Wed" => 67,
"Thu" => 62, "Fri" => 65);
$days = array_keys($highs);
$temps = array_values($highs);
• Dealing with Arrays
– An array can be deleted with unset
unset($list);
unset($list[4]); # No index 4 element now
34

ARRAYS
• Dealing with Arrays (continued)
– is_array($list) returns true if $list is a function
– in_array(17, $list) returns true if 17 is an element of $list
– explode(" ", $str) creates an array with the values of the
words from $str, split on a space
– implode(" ", $list) creates a string of the elements from $list,
separated by a space
• Sequential access to array elements
– current and next
$colors = array("Blue", "red", "green","yellow");
$color = current($colors);
print("$color <br />");
while ($color = next($colors))
print ("$color <br />");
35

ARRAYS
• This does not always work – for example, if the value in the
array happens to be FALSE
– Alternative: each, instead of next
while ($element = next($colors)) { print
("$element['value'] <br />");
• The prev function moves current backwards
array_push($list, $element) and array_pop($list)
– Used to implement stacks in arrays
– foreach (array_name as scalar_name) { ... }
foreach ($colors as $color) {print "Is $color your
favorite?<br /> ";}
Is red your favorite color
Is blue your favorite color
Is green your favorite color?
Is yellow your favorite color?
36

ARRAYS
• foreach can iterate through both keys and values:
foreach (array as key => value) { … }
– Inside the compound statement, both $key and $color are defined
$ages = array("Bob" => 42, "Mary" => 43);
foreach ($ages as $name => $age)
print("$name is $age years old <br />");
37

PHP EXAMPLES
• To concatenate two or more variables together, use the dot
(.) operator:

<html>
<body><?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2 ;
?></body>
</html>

• The output of the script above will be: "Hello World 1234"
38

PHP EXAMPLES
• The following example will output "Have a nice weekend!" if
the current day is Friday, otherwise it will output "Have a
nice day!":
<html>
<body><?php
$d=date("D");
if ($d =="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?></body>
</html>
39

PHP EXAMPLES
• The following example prints the text "Hello World!" five
times:

<html>
<body><?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?></body>
</html>
40

PHP EXAMPLES
• The following example demonstrates a loop that will print
the values of the given array:
<html>
<body><?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?></body>
</html>

You might also like