1 / 74

php

php. What is PHP?. PHP is an acronym for "PHP Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP costs nothing, it is free to download and use. What is a PHP File?.

gfeltner
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

  2. What is PHP? • PHP is an acronym for "PHP Hypertext Preprocessor" • PHP is a widely-used, open source scripting language • PHP scripts are executed on the server • PHP costs nothing, it is free to download and use

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

  4. 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.

  5. Why PHP? • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) • PHP is compatible with almost all servers used today (Apache, IIS, etc.) • PHP supports 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

  6. Install PHP • What Do I Need? • To start using PHP, you can: • Find a web host with PHP and MySQL support • Install a web server on your own PC, and then install PHP and MySQL

  7. Go to http://www.apachefriends.org/en/xampp-windows.html Download XAMPP Windows 1.8.3 installer

  8. PHP 5 Syntax <!DOCTYPE html><html><body><h1>My first PHP page</h1><?phpecho "Hello World!";?>  </body></html>

  9. Comments in PHP • <!DOCTYPE html><html><body><?php// This is a single line comment# This is also a single line comment/*This is a multiple lines comment blockthat spans over more thanone line*/?></body></html>

  10. PHP Case Sensitivity • In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-insensitive. • In the example below, all three echo statements below are legal (and equal): <!DOCTYPE html><html><body><?phpECHO "Hello World!<br>";echo "Hello World!<br>";EcHo "Hello World!<br>";?></body></html>

  11. However; in PHP, all variables are case-sensitive. • <!DOCTYPE html><html><body><?php$color="red";echo "My car is " . $color . "<br>";echo "My house is " . $COLOR . "<br>";echo "My boat is " . $coLOR . "<br>";?></body></html>

  12. PHP 5 Variables Variables are "containers" for storing information 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 ($y and $Y are two different variables)

  13. <!DOCTYPE html><html><body><?php$txt="Hello world!";$x=5;$y=10.5;echo $txt;echo "<br>";echo $x;echo "<br>";echo $y;?></body></html>

  14. PHP Variables Scope • In PHP, variables can be declared anywhere in the script. • The scope of a variable is the part of the script where the variable can be referenced/used. • PHP has three different variable scopes: • local • global • static

  15. Local and Global Scope • A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. • A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

  16. <!DOCTYPE html><html><body><?php$x=5; // global scopefunction myTest(){$y=10; // local scopeecho "<p>Test variables inside the function:<p>";echo "Variable x is: $x";echo "<br>";echo "Variable y is: $y";} myTest();echo "<p>Test variables outside the function:<p>";echo "Variable x is: $x";echo "<br>";echo "Variable y is: $y";?></body></html>

  17. PHP The global Keyword <!DOCTYPE html><html><body><?php$x=5;$y=10;function myTest(){global $x,$y;$y=$x+$y;} myTest(); // run functionecho $y; // output the new value for variable $y?></body></html>

  18. <!DOCTYPE html><html><body><?php$x=5;$y=10;function myTest(){$GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];} myTest();echo $y;?></body></html>

  19. PHP The static Keyword • Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

  20. <!DOCTYPE html><html><body><?phpfunction myTest(){static $x=0;echo $x;$x++;}myTest();echo "<br>";myTest();echo "<br>";myTest();echo "<br>";myTest();echo "<br>";myTest();?>  </body></html>

  21. PHP 5 echo and print Statements In PHP there is two basic ways to get output: echo and print. There are some difference between echo and print: • echo - can output one or more strings • print - can only output one string, and returns always 1

  22. The PHP echo Statement Display Strings <!DOCTYPE html><html><body><?phpecho "<h2>PHP is fun!</h2>";echo "Hello world!<br>";echo "I'm about to learn PHP!<br>";echo "This", " string", " was", " made", " with multiple parameters.";?>  </body></html>

  23. Display Variables <!DOCTYPE html><html><body><?php$txt1="Learn PHP";$txt2="W3Schools.com";$cars=array("Volvo","BMW","Toyota");echo $txt1;echo "<br>";echo "Study PHP at $txt2";echo "<br>";echo "My car is a {$cars[0]}";?></body></html>

  24. The PHP print Statement • Display Strings <!DOCTYPE html><html><body><?phpprint "<h2>PHP is fun!</h2>";print "Hello world!<br>";print "I'm about to learn PHP!";?>  </body></html>

  25. Display Variables <!DOCTYPE html><html><body><?php$txt1="Learn PHP";$txt2="W3Schools.com";$cars=array("Volvo","BMW","Toyota");print $txt1;print "<br>";print "Study PHP at $txt2";print "<br>";print "My car is a {$cars[0]}";?></body></html>

  26. PHP Data Types • String, Integer, Floating point numbers, Boolean, Array, Object, NULL.

  27. PHP Strings • <!DOCTYPE html><html><body><?php$x = "Hello world!";echo $x;echo "<br>"; $x = 'Hello world!';echo $x;?></body></html>

  28. PHP Integers • An integer is a number without decimals. • Rules for integers: • An integer must have at least one digit (0-9) • An integer cannot contain comma or blanks • An integer must not have a decimal point • An integer can be either positive or negative • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)

  29. <!DOCTYPE html><html><body><?php$x = 5985;var_dump($x);echo "<br>"; $x = -345; // negative number var_dump($x);echo "<br>"; $x = 0x8C; // hexadecimal numbervar_dump($x);echo "<br>";$x = 047; // octal numbervar_dump($x);?>   </body></html>

  30. PHP Floating Point Numbers • <!DOCTYPE html><html><body><?php$x = 10.365;var_dump($x);echo "<br>"; $x = 2.4e3;var_dump($x);echo "<br>"; $x = 8E-5;var_dump($x);?>   </body></html>

  31. PHP Booleans • Booleans can be either TRUE or FALSE. • var x=true;var y=false; • Booleans are often used in conditional testing.

  32. PHP Arrays • <!DOCTYPE html><html><body><?php$cars=array("Volvo","BMW","Toyota");var_dump($cars);?>   </body></html>

  33. PHP Objects • An object is a data type which stores data and information on how to process that data. • In PHP, an object must be explicitly declared. • First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods. • We then define the data type in the object class, and then we use the data type in instances of that class:

  34. <!DOCTYPE html><html><body><?phpclass Car{var $color;    function Car($color="green")     {        $this->color = $color;    }    function what_color()     {        return $this->color;    }}function print_vars($obj) {foreach (get_object_vars($obj) as $prop => $val)   {     echo "\t$prop = $val\n";   }}// instantiate one object$herbie = new Car("white");// show herbie propertiesecho "\herbie: Properties\n";print_vars($herbie);?></body></html>

  35. PHP NULL Value • <!DOCTYPE html><html><body><?php$x="Hello world!";$x=null;var_dump($x);?></body></html>

  36. PHP String Functions • The PHP strlen() function <!DOCTYPE html><html><body><?phpecho strlen("Hello world!");?>  </body></html>

  37. The PHP strpos() function <!DOCTYPE html><html><body><?phpecho strpos("Hello world!","world");?>  </body></html>

  38. For a complete reference of all string functions go to http://www.w3schools.com/php/php_ref_string.asp

  39. PHP Constants • A constant is an identifier (name) for a simple value. The value cannot be changed during the script. • A valid constant name starts with a letter or underscore (no $ sign before the constant name). • Note: Unlike variables, constants are automatically global across the entire script.

  40. Set a PHP Constant • To set a constant, use the define() function - it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be case-insensitive. Default is false.

  41. <!DOCTYPE html><html><body><?php// define a case-sensitive constantdefine("GREETING", "Welcome to W3Schools.com!");echo GREETING;echo "<br>";// will not output the value of the constantecho greeting;?>  </body></html>

  42. <!DOCTYPE html><html><body><?php// define a case-insensitive constantdefine("GREETING", "Welcome to W3Schools.com!", true);echo GREETING;echo "<br>";// will also output the value of the constantecho greeting;?>  </body></html>

  43. PHP Operators PHP Arithmetic Operators

  44. PHP Assignment Operators The PHP assignment operators is used to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

  45. PHP String Operators The example below shows the results of using the string operators:

  46. PHP Increment / Decrement Operators

  47. <?php$x=10; echo ++$x; // outputs 11$y=10; echo $y++; // outputs 10$z=5;echo --$z; // outputs 4$i=5;echo $i--; // outputs 5?>

  48. PHP Comparison Operators The PHP comparison operators are used to compare two values (number or string):

  49. <?php$x=100; $y="100";var_dump($x == $y); // returns true because values are equalecho "<br>";var_dump($x === $y); // returns false because types are not equalecho "<br>";var_dump($x != $y); // returns false because values are equalecho "<br>";var_dump($x !== $y); // returns true because types are not equalecho "<br>";$a=50;$b=90;var_dump($a > $b);echo "<br>";var_dump($a <  $b);?>

  50. PHP Logical Operators

More Related