1 / 59

php

php. Hypertext Preprocessor. What is a PHP File?. PHP files can contain text, HTML, JavaScript code, and PHP code PHP code are executed on the server, and the result is returned to the browser as plain HTML PHP files have a default file extension of ".php". What Can PHP Do?.

mariel
Download Presentation

php

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. php Hypertext Preprocessor

  2. What is a PHP File? • PHP files can contain text, HTML, JavaScript code, and PHP code • PHP code are executed on the server, and the result is returned to the browser as plain HTML • PHP files have a default file extension of ".php"

  3. What Can PHP Do? • PHP can generate dynamic page content. • PHP can create, open, read, write, and close files on the server. • PHP can collect form data. • PHP can send and receive cookies. • PHP can add, delete, modify data in your database. • PHP can restrict users to access some pages on your website. • PHP can encrypt data. • With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

  4. Why PHP? • PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.) • PHP is compatible with almost all servers used today (Apache, IIS, etc.) • PHP has support for a wide range of databases • PHP is free. Download it from the official PHP resource: www.php.net • PHP is easy to learn and runs efficiently on the server side

  5. Basic PHP Syntax A PHP script starts with <?php and ends with ?>: <html><body><h1>My first PHP page</h1><?phpecho “Welcome Alpha!";?></body></html>

  6. Comment in PHP <!DOCTYPE html><html><body><?php//This is a PHP comment line/*This is a PHP commentblock*/?></body></html>

  7. Rules for Variables in PHP • As with algebra, PHP variables can be used to hold values (x=5) or expressions (z=x+y). • Variable can have short names (like x and y) or more descriptive names (age, carname, totalvolume). • Rules for PHP variables: • A variable starts with the $ sign, followed by the name of the variable • A variable name must begin with a letter or the underscore character • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) • A variable name should not contain spaces • Variable names are case sensitive ($y and $Y are two different variables)

  8. Variables in PHP <?php$x=5;$y=6;$z=$x+$y;echo $z;?> x=5y=6z=x+y In algebra we use letters (like x) to hold values (like 5). From the expression z=x+y above, we can calculate the value of z to be 11. In PHP these letters are called VARIABLES.

  9. PHP is a Loosely Typed Language • $txt="Hello world!";$x=5; • PHP automatically converts the variable to the correct data type, depending on its value. • In a strongly typed programming language, we will have to declare (define) the type and name of the variable before using it.

  10. PHP Variable Scopes • PHP has four different variable scopes: • Local scope • Global scope • Static scope • Parameter scope

  11. Local Scopes <?php$x=5; // global scopefunction myTest(){echo $x; // local scope}myTest();?>

  12. Global Scopes <?php$x=5; // global scope$y=10; // global scopefunctionmyTest(){global $x,$y;$y=$x+$y;}myTest();echo $y; // output is 15?>

  13. Static Scopes <?phpfunction myTest(){static $x=0;echo $x;$x++;}myTest();myTest();myTest();?>

  14. Parameter Scopes • A parameter is a local variable whose value is passed to the function by the calling code. • Parameters are declared in a parameter list as part of the function declaration: <?phpfunction myTest($x){echo $x;}myTest(5);?>

  15. String Variables in PHP • String variables are used for values that contain characters. • After we have created a string variable we can manipulate it. A string can be used directly in a function or it can be stored in a variable. • In the example below, we create a string variable called txt, then we assign the text "Hello world!" to it. Then we write the value of the txt variable to the output: <?php$txt="Hello world!";echo $txt;?>

  16. Concatenation String Operators • There is only one string operator in PHP. • The concatenation operator [ . ]  is used to join two string values together. <?php$txt1="Hello world!";$txt2="What a nice day!";echo $txt1 . " " . $txt2;?>

  17. PHP strlen() function Sometimes it is useful to know the length of a string value. The strlen() function returns the length of a string, in characters. <?phpecho strlen("Hello world!");?>

  18. PHP strpos() function • The strpos() function is used to search for a character or a specific text within a string. • If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE. <?phpecho strpos("Hello world!","world");?>

  19. PHP Date() function <?phpprint(“Today’s Date is ”.date(“l F d, y”)); ?>

  20. PHP Arithmetic Operators

  21. PHP Assignment Operators • The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the expression on the right. That is, the value of "$x = 5" is 5.

  22. PHP Incr/Decr Operators

  23. PHP Comparison Operators

  24. PHP Logical Operators

  25. PHP Array Operators

  26. PHP If Statements • SYNTAX: • if (condition)  {  code to be executed if condition is true;} <?php$t=date("H");if ($t<"20")  {  echo "Have a good day!";  }?>

  27. PHP If…Else Statements • SYNTAX: • if (condition)  {  code to be executed if condition is true;} • else {  code to be executed if condition is false; }

  28. PHP If…Else Statements (cont…) <?php$t=date("H");if ($t<"20")  {  echo "Have a good day!";  }else  {  echo "Have a good night!";  }?>

  29. PHP If…Else If…Else Statements SYNTAX: if (condition)  {  code to be executed if condition is true;  }else if (condition)  {  code to be executed if condition is true; }else  {  code to be executed if condition is false; }

  30. PHP If…Else If…Else Statements (cont…) <?php$t=date("H");if ($t<"10")  {  echo "Have a good morning!";  }else if ($t<"20")  {  echo "Have a good day!";  }else  {  echo "Have a good night!";  }?>

  31. PHP Switch Case Statements SYNTAX: switch (n){case label1:   code to be executed if n=label1;  break;case label2:   code to be executed if n=label2;   break;default:   code to be executed if n is different from both label1 and label2;}

  32. PHP Switch Case Statements <?php$favcolor="red";switch ($favcolor){case "red":  echo "Your favorite color is red!“;  break;case "blue":  echo "Your favorite color is blue!“;  break;case "green":  echo "Your favorite color is green!“;  break;default:  echo "Your favorite color is None of them!";}?>

  33. PHP Arrays • An array is a special variable, which can hold more than one value at a time. • If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: $cars1="Volvo";$cars2="BMW";$cars3="Toyota"; • An array can hold many values under a single name, and you can access the values by referring to an index number.

  34. PHP Arrays Creating a php Arrays • In PHP, the array() function is used to create an array: Types of php Arrays In PHP, there are three types of arrays: Indexed arrays - Arrays with numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays

  35. PHP Indexed Arrays • There are two ways to create indexed arrays: • The index can be assigned automatically (index always starts at 0): $cars=array("Volvo","BMW","Toyota"); • The index can be assigned manually: $cars[0]="Volvo";$cars[1]="BMW";$cars[2]="Toyota"; <?php$cars=array("Volvo","BMW","Toyota");echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";?>

  36. PHP Associative Arrays • Associative arrays are arrays that use named keys that you assign to them. • There are two ways to create an associative array:  $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); $age['Peter']="35";$age['Ben']="37";$age['Joe']="43"; <?php$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");echo "Peter is " . $age['Peter'] . " years old.";?>

  37. PHP Multidimensional Arrays • A multidimensional array is an array containing one or more arrays. • PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people. <?php$cars = array   (   array("Volvo",22,18),   array("BMW",15,13),   array("Saab",5,2),); echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";?>

  38. PHP Arrays Sorting Ascending sort() - sort arrays in ascending order <?php$cars=array("Volvo","BMW","Toyota");sort($cars);$clength=count($cars);for($x=0;$x<$clength;$x++)   {   echo $cars[$x];   echo "<br>";   }?>

  39. PHP Arrays Sorting Descending rsort() - sort arrays in descending order <?php$cars=array("Volvo","BMW","Toyota");rsort($cars);$clength=count($cars);for($x=0;$x<$clength;$x++)   {   echo $cars[$x];   echo "<br>";   }?>

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

  41. PHP While & Do-While Loop <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br>"; $i++; } ?> <?php$x=1; do  {  echo "The number is: $x <br>";  $x++;  }while ($x<=5)?>

  42. PHP User Defined Functions • Besides the built-in PHP functions, we can create our own functions. • A function is a block of statements that can be used repeatedly in a program. • A function will not execute immediately when a page loads. • A function will be executed by a call to the function. <?php echo "-----------Simple Functions-----------"; function writeName() { echo "Amit Patel"; } echo "<br/>My name is "; writeName(); echo "<br/>"; echo "<br/>"; ?>

  43. PHP User Defined Functions <?php echo "--------Functions: Adding Parameters-------"; function write_Name($fname) { echo $fname . " Patel.<br>"; } echo "<br/>My name is "; write_Name("Amit"); echo "My Friend name is "; write_Name("Rakesh"); echo "My Friend name is "; write_Name("Sanjay"); echo "<br/>"; ?>

  44. PHP User Defined Functions <?php echo "----------Functions: Returns Values---------"; echo "<br/>"; function add($x,$y) { $total=$x+$y; return $total; } echo "1 + 16 = " . add(1,16); ?>

  45. PHP Server Connectivity Use the PHP mysqli_connect() function to open a new connection to the MySQL server. Syntax mysqli_connect(host,username,password,dbname);

  46. PHP Server Connectivity Script for Connecting Server <?php// Create connection$con=mysqli_connect("example.com","peter","abc123","my_db");// Check connectionif (mysqli_connect_errno())  {  echo "Failed to connect to MySQL: " . mysqli_connect_error();  }?>

  47. PHP Server Connectivity Script for Disconnecting Server <?php$con=mysqli_connect("example.com","peter","abc123","my_db");// Check connectionif (mysqli_connect_errno())  {  echo "Failed to connect to MySQL: " . mysqli_connect_error();  }mysqli_close($con);?>

  48. PHP Creating Database <?php $con=mysqli_connect("localhost","root",""); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Create database $sql="CREATE DATABASE temp2"; if (mysqli_query($con,$sql)) { echo "Database temp2 created successfully"; } else { echo "Error creating database: " . mysqli_error(); } ?>

More Related