Introduction to PHP Introduction to PHP Hypertext Preprocessor

  • Slides: 45
Download presentation
Introduction to PHP

Introduction to PHP

Introduction to PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language That was

Introduction to PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language That was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, PHP was originally created by Rasmus Lerdorf in 1995[ and has been in continuous development ever since.

Continue Introduction to PHP The main implementation of PHP is now produced by the

Continue Introduction to PHP The main implementation of PHP is now produced by the PHP Group and serves as the de facto standard PHP as there is no formal specification. [PHP is free software released under the PHP License. PHP originally stood for personal home page. Its development began in 1994 when the Danish/Greenlandic programmer Rasmus Lerdorf initially created a set of Perl scripts he called 'Personal Home Page Tools' to maintain his personal homepage

Continue Introduction to PHP Its development began in 1994 when the Danish/Greenlandic programmer Rasmus

Continue Introduction to PHP Its development began in 1994 when the Danish/Greenlandic programmer Rasmus Lerdorf initially created a set of Perl scripts he called 'Personal Home Page Tools' Rasmus Lerdorf

PHP Syntax l l l Basic PHP Syntax A PHP script always starts with

PHP Syntax l l l Basic PHP Syntax A PHP script always starts with <? php and ends with ? >. A PHP script can be placed anywhere in the document.

How To Code PHP Programe ? Step How To execute Your Code In Browser

How To Code PHP Programe ? Step How To execute Your Code In Browser 1. Open Editor Any 2. Write Code For PHP 3. Save The Code Any Name Ex: -<file Name>. php hello. php 4. Save the Speceific Path in wamp Server For Example: hello. php is Save in wamp Server Don’t Save Your Local Hardisk any Other Save The Specefic Location l

Simple Php Programe <html> <body> <? php echo "Hello World"; ? > </body> </html>

Simple Php Programe <html> <body> <? php echo "Hello World"; ? > </body> </html>

How To Comment In PHP ? <html> <body> <? php //This is a comment

How To Comment In PHP ? <html> <body> <? php //This is a comment /* This is a comment block */ ? > </body> </html>

PHP Variables A variable is a means of storing a value, such as text

PHP Variables A variable is a means of storing a value, such as text string For Ex: -string "Hello World!" or the integer value 4. In PHP you define a variable with the following form: $variable_name = Value; Note: -All variables in PHP start with a $ sign symbol.

PHP Variables are "containers" for storing information. The correct way of declaring a variable

PHP Variables are "containers" for storing information. The correct way of declaring a variable in PHP: $var_name = value; Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes. For Ex: <? php $txt="Hello World!"; $x=16; ? >

Naming Rules for Variables PHP variables must start with a letter or underscore "_".

Naming Rules for Variables PHP variables must start with a letter or underscore "_". PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0 -9, or _. Variables with more than one word should be separated with underscores. $my_variable Variables with more than one word can also be distinguished with capitalization. $my. Variable

PHP Variable Scope l l l The scope of a variable is the portion

PHP Variable Scope l l l The scope of a variable is the portion of the script in which the variable can be referenced. PHP has four different variable scopes: local global static parameter

Local Scope l A variable declared within a PHP function is local and can

Local Scope l A variable declared within a PHP function is local and can only be accessed within that function. l <? php $a = 5; // global scope function my. Test() { echo $a; // local scope } my. Test(); ? >

Global Scope Global scope refers to any variable that is defined outside of any

Global Scope Global scope refers to any variable that is defined outside of any function. To access a global variable from within a function, use the global keyword: <? php $a = 5; $b = 10; function my. Test() { global $a, $b; $b = $a + $b; } my. Test(); echo $b; ? >

Static Scope When a function is completed, all of its variables are normally deleted.

Static Scope When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted. l To do this, use the static keyword when you first declare the variable: Ex: static $remember. Me; l

Parameters A parameter is a local variable whose value is passed to the function

Parameters A parameter is a local variable whose value is passed to the function by the calling code. function my. Test($para 1, $para 2, . . . ) { // function code }

PHP is a Loosely Typed Language l l l In PHP, a variable does

PHP is a Loosely Typed Language l l l 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.

PHP is a Loosely Typed Language For Example: <? php $a=“tybca”; echo $a; $b=5;

PHP is a Loosely Typed Language For Example: <? php $a=“tybca”; echo $a; $b=5; echo $b; $c=5. 5; echo $c; ? >

Environment Variables PHP has a collection of environment variables, which are system defined variables

Environment Variables PHP has a collection of environment variables, which are system defined variables that are accessible from anywhere inside the PHP code. All of these environment variables are stored by PHP as arrays. 1. $_SERVER : -Contains information about the server and the HTTP connection. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated). 2. $_COOKIEC : -ontains any cookie data sent back to the server from the client. Indexed by cookie name. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated). 3. $_GET: - Contains any information sent to the server as a search string as part of the URL. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).

Environment Variables 4. $_POST: -Contains any information sent to the server as a POST

Environment Variables 4. $_POST: -Contains any information sent to the server as a POST style posting from a client form. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated). 5. $ _FILE: -Contains information about any uploaded files. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated) 6. $_ENV: -Contains information about environmental variables on the server. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).

Web Protocol HTTP The Hypertext Transfer Protocol (HTTP) is a networking protocol for distributed,

Web Protocol HTTP The Hypertext Transfer Protocol (HTTP) is a networking protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. The standards development of HTTP has been coordinated by the Internet Engineering Task Force (IETF) and the World Wide Web Consortium, culminating in the publication of a series of Requests for Comments (RFCs), most notably RFC 2616 (June 1999), which defines HTTP/1. 1, the version of HTTP in common use.

Web Protocol HTTP functions as a request-response protocol in the clientserver computing model. In

Web Protocol HTTP functions as a request-response protocol in the clientserver computing model. In HTTP, a web browser for example: , while an application running on a computer hosting a web site functions as a server. The client submits an HTTP request message to the server. The server, which stores content, or provides resources, such as HTML files and images, or generates such content as required, or performs other functions on behalf of the client, returns a response message to the client. A response contains completion status information about the request and may contain any content requested by the client in its message body. HTTP is an Application Layer protocol designed within the framework of the Internet Protocol Suite.

Php Constants variables l . . values that never changes l Constants are defined

Php Constants variables l . . values that never changes l Constants are defined in PHP by using the define() function. – For e. g. define(“TYBCA”, “PROF. AHESANALI”) l defined() function says whether the constant exists or not l Using By function defined()

//File Name As hello. php //Simple Programe To Display Name <? php echo “<Enter

//File Name As hello. php //Simple Programe To Display Name <? php echo “<Enter Your name>”; Print “hello”; Print_r “hello”; ? >

PHP Operators: l l l Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Operator

PHP Operators: l l l Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Operator Precedence: -

Conditional structure and looping structure: l l l IF. . . Else Statement Switch

Conditional structure and looping structure: l l l IF. . . Else Statement Switch Statement Type conversion rules to Boolean

If. . . Else Statement The If Statement is a way to make decisions

If. . . Else Statement The If Statement is a way to make decisions based upon the result of a condition. l The syntax for If statement looks as follows: if (condition) { statements_1 } else { statements_2 }

If. . . Else Statement <? php $result = 70; if ($result >= 75)

If. . . Else Statement <? php $result = 70; if ($result >= 75) { echo "Passed: Grade A "; } elseif ($result >= 60) { echo "Passed: Grade B "; } elseif ($result >= 45) { echo "Passed: Grade C "; } else { echo "Failed "; } ? >

Switch Statement Switch statements work the same as if statements. However the difference is

Switch Statement Switch statements work the same as if statements. However the difference is that they can check for multiple values. switch (expression) { case label_1: statements_1 [break; ] case label_2: statements_2 [break; ]. . . default: statements_n [break; ] }

Switch Statement <? php $flower = "rose"; switch ($flower) { case "rose" : echo

Switch Statement <? php $flower = "rose"; switch ($flower) { case "rose" : echo $flower. " costs $2. 50"; break; case "daisy" : echo $flower. " costs $1. 25"; break; case "orchild" : echo $flower. " costs $1. 50"; break; default : echo "There is no such flower in our shop"; break; } ? >

Switch Statement <? php $my. Number = 5; switch ($my. Number) { case 0:

Switch Statement <? php $my. Number = 5; switch ($my. Number) { case 0: echo "Zero is not a valid value. "; break; case $my. Number < 0: echo "Negative numbers are not allowed. "; break; default: echo "Great! Ready to make calculations. "; break; } ? >

Type conversion rules to Boolean When converting to Boolean, the following values are considered

Type conversion rules to Boolean When converting to Boolean, the following values are considered FALSE: – – – – boolean False integer zero (0) double zero (0. 0) empty string and string "0" array with no elements object with no elements special value NULL Every other value is considered TRUE.

Looping Structure: In programming it is often necessary to repeat the same block of

Looping Structure: In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements. l l l The While Loop The Do. . . While Loop The Foreach Loop Break and Continue Statements

The While Loop The While statement executes a block of code if and as

The While Loop The While statement executes a block of code if and as long as a specified condition evaluates to true. If the condition becomes false, the statements within the loop stop executing and control passes to the statement following the loop while (condition) { code to be executed; }

The While Loop <? php $i=0; while ($i <= 10) { // Output values

The While Loop <? php $i=0; while ($i <= 10) { // Output values from 0 to 10 echo "The number is ". $i. " "; $i++; } ? >

The Do. . . While Loop The Do. . . While statements are similar

The Do. . . While Loop The Do. . . While statements are similar to While statements, except that the condition is tested at the end of each iteration, rather than at the beginning. This means that the Do. . . While loop is guaranteed to run at least once. do { code to be exected; }

The Do. . . While Loop <? php $i = 0; do { echo

The Do. . . While Loop <? php $i = 0; do { echo "The number is ". $i. "<br/>"; $i++; } while ($i <= 10); ? >

The For Loop The For statement loop is used when you know how many

The For Loop The For statement loop is used when you know how many times you want to execute a statement or a list of statements. for (initialization; condition; increment) { code to be executed; }

The For Loop <? php for ($i=0; $i <= 10; $i++) { echo "The

The For Loop <? php for ($i=0; $i <= 10; $i++) { echo "The number is ". $i. " "; } ? >

The Foreach Loop The Foreach loop is a variation of the For loop and

The Foreach Loop The Foreach loop is a variation of the For loop and allows you to iterate over elements in an array. There are two different versions of the Foreach loop. The Foreach loop syntaxes are as follows: - 1. foreach (array as value) { code to be executed; } 2. foreach (array as key => value) { code to be executed; }

1. The Foreach Loop <? php $email = array(‘mmomin 79@yahoo. com’, ‘mmomin 79@gmail. com’);

1. The Foreach Loop <? php $email = array(‘mmomin 79@yahoo. com’, ‘mmomin 79@gmail. com’); foreach ($email as $value) { echo "Processing ". $value. " "; } ? >

2. The Foreach Loop <? php $person = array('name' => ‘momin', 'age' => 23,

2. The Foreach Loop <? php $person = array('name' => ‘momin', 'age' => 23, 'address' => ‘ 7, momin society. '); foreach ($person as $key => $value) { echo $key. " is ". $value. " "; } ? >

Break and Continue Statements Sometimes you may want to let the loops start without

Break and Continue Statements Sometimes you may want to let the loops start without any condition, and allow the statements inside the brackets to decide when to exit the loop. There are two special statements that can be used inside loops: Break and Continue. <? php echo "<p><b>Example of using the Break statement: </b></p>"; for ($i=0; $i<=10; $i++) { if ($i==3){break; } echo "The number is ". $i; echo " "; }

Break and Continue Statements $i = 0; $j = 0; while ($i < 10)

Break and Continue Statements $i = 0; $j = 0; while ($i < 10) { while ($j < 10) { if ($j == 5) {break 2; } // breaks out of two while loops $j++; } $i++; } echo "The first number is ". $i. " "; echo "The second number is ". $j. " "; ? >

Break and Continue Statements <? php echo "<p><b>Example of using the Continue statement: </b><p>";

Break and Continue Statements <? php echo "<p><b>Example of using the Continue statement: </b><p>"; for ($i=0; $i<=10; $i++) { if (i==3){continue; } echo "The number is ". $i; echo " "; } ? >