1 / 109

Chapter - 1

Chapter - 1. The Basic PHP. PHP: Hypertext Preprocessor. Outline. What is PHP ? History of PHP Why PHP ? What is PHP file? What you need to start using PHP ? Syntax PHP code . echo & print Statement Variables. Data Types. Constants &Operators. Conditional Statements & Loops.

morna
Download Presentation

Chapter - 1

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. Chapter - 1 The Basic PHP PHP: Hypertext Preprocessor

  2. Outline • Whatis PHP? • History of PHP • Why PHP ? • Whatis PHP file? • What you need to start using PHP ? • Syntax PHP code . • echo & print Statement • Variables. • Data Types. • Constants &Operators. • Conditional Statements & Loops.

  3. Whatis PHP? • Personal Homepage Tools/Form Interpreter • PHP is a Server-side Scripting Languagedesigned specifically for the Web. • An open source language • PHP code can be embedded within an HTML page, which will be executed each time that page is visited.

  4. Whatis PHP? (cont’d) • Interpreted language, scripts are parsed at run-time rather than compiled beforehand • Executed on the server-side • Source-code not visible by client • ‘View Source’ in browsers does not display the PHP code • Various built-in functions allow for fast development • Compatible with many popular databases

  5. History of PHP • PHP (PHP: Hypertext Preprocessor) was created by RasmusLerdorf in 1994. It was initially developed for HTTP usage logging and server-side form generation in Unix. • PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. • PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by ZeevSuraski and AndiGutmans . • PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. • PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP

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

  7. What does PHP code look like? • Structurally similar to C/C++ • Supports procedural and object-oriented paradigm (to some degree)

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

  9. 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”

  10. What you need to start using PHP ? • Installation • You will need • Web server ( Apache ) • PHP ( version 5.3) • Database ( MySQL 5 ) • Text editor (Notepad) • Web browser (Firefox ) • www.php.net/manual/ en/install.php

  11. Syntax PHP code • A PHP script can be placed anywhere in the document. • A PHP script starts with <?php and ends with ?>

  12. Syntax PHP code • Standard Style : <?php …… ?> • Short Style: <? … ?> • Script Style: <SCRIPT LANGUAGE=‘php’> </SCRIPT> • ASP Style: <% echo “Hello World!”; %>

  13. Echo • The PHP command ‘echo’ is used to output the parameters passed to it . • The typical usage for this is to send data to the client’s web-browser • Syntax : void echo (string arg1 [, string argn...]) • In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function

  14. Echo - Example • <?php echo “ This my first statement in PHP language“; • ?>

  15. Print • print is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list. <?php print("Hello World"); ?>

  16. Echo Vs Print

  17. Variables • 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

  18. Variables • 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)

  19. Variables • Case-sensitive ($Foo != $foo != $fOo) • Global and locally-scoped variables • Global variables can be used anywhere • Local variables restricted to a function or class • Certain variable names reserved by PHP • Form variables ($_POST, $_GET) • Server variables ($_SERVER)

  20. Creating (Declaring) Variables <?php $name = “ali” echo( $name); ?>

  21. Creating (Declaring) Variables • PHP has no command for declaring a variable. • A variable is created the moment you first assign a value to it: • After the execution of the statements above, the variable txt will hold the value Hello world!, and the variable xwill hold the value 5. • Note: When you assign a text value to a variable, put quotes around the value. $txt="Hello world!";$x=5;

  22. Variables <?php $name = “ali”; $age = 23; echo “ My name is $name and I am $age years old”; ?>

  23. PHP is a Loosely Typed Language • In the example above, notice that we did 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, we will have to declare (define) the type and name of the variable before using it.

  24. Variables • <?php • $name = 'elijah'; • $yearborn = 1975; • $currentyear = 2005; • $age = $currentyear - $yearborn; • echo ("$name is $age years old."); • ?>

  25. Variables <?php $name = “Ali"; // declaration ?> <html> <head> <title>A simple PHP document</title> </head> <body style = "font-size: 2em"> <p> <strong> <!-- print variable name’s value --> Welcome to PHP, <?php echo( "$name" ); ?>! </strong> </p> </body> </html>

  26. PHP Variable Scopes • The scope of a variable is the part of the script where the variable can be referenced/used. • PHP has four different variable scopes: • local • global • static • Parameter - In chapter function we will talk about theme.

  27. 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;?>

  28. PHP strings can be specified in four ways • Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed). <?php $txt = ‘my string ‘; echo ‘$txt’; // $txt ?> <?php $txt = ‘my string ‘; echo $txt; // my string ?>

  29. PHP strings can be specified in four ways • Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you what to echo "The $types are" That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such. <?php $txt = “my string”; echo $txt; // my string ?> <?php $txt = “my string “; echo “$txt”; // my string ?>

  30. PHP strings can be specified in four ways • Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax. • Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

  31. PHP strings can be specified in four ways • Heredoc <?php $name='MyName'; echo <<<EOT My name is "$name". I am printing some A Now, I am printing some {A}. This should print a capital 'A': \x41 EOT; ?> • My name is "MyName". I am printing some A Now, • I am printing some {A}. • This should print a capital 'A': A

  32. PHP strings can be specified in four ways • Nowdoc • <?php • $name='MyName'; • echo <<<'EOT' • My name is "$name". • I am printing some A Now, • I am printing some {A}. • This should print a capital 'A': \x41 • EOT; • ?> My name is "$name". I am printing some A Now, I am printing some {A}. This should print a capital 'A': \x41

  33. Single & Double Quotes <?php echo “ Hello world <br>”; echo ‘ Hello world’; ?>

  34. Single & Double Quotes <?php $word = ‘ World’; echo “ Hello $word <br>”; echo ‘ Hello $word <br>’; ?>

  35. Comments in PHP • // or # for single line • /* */ for multiline • /* this is my comment one this is my comment two this is my comment three */

  36. Whitespace • You cant have any whitespace between <? and php. • You cant break apart keywords (e.g :while,function,fo r) • You cant break apart varible names and function names (e.g:$varname,function f 2)

  37. The PHP Concatenation Operator • here is only one string operator in PHP. • The concatenation operator (.) is used to join two string values together. • The example below shows how to concatenate two string variables together: <?php $txt1="Hello!"; $txt2=" world !"; echo $txt1 . " " . $txt2; // Hello world !?>

  38. The PHP Concatenation Operator • <?php • $string1=“Hello”; • $string2=“PHP”; • $string3=$string1 . “ ” . $string2; • Print $string3; • ?> Hello PHP

  39. Escaping the Character • If the string has a set of double quotation marks that must remain visible, use the \ [backslash] before the quotation marks to ignore and display them. <?php $heading="\"Computer Science\""."<br>"; $heading1=@"Computer Science"; echo $heading; echo $heading1; ?> "Computer Science"Computer Science 

  40. Example • <?php • $foo = 25; // Numerical variable$bar = “Hello”; // String variable • echo $bar; // Outputs Hello • echo $foo,$bar; // Outputs 25Hello • echo “5x5=”,$foo; // Outputs 5x5=25 • echo “5x5=$foo”; // Outputs 5x5=25echo ‘5x5=$foo’; // Outputs 5x5=$foo • ?> • Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25 • Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP • This is true for both variables and character escape-sequences (such as “\n” or “\\”)

  41. Data type

  42. Get type • gettype — Get the type of a variable • Returns the type of the PHP variable var. <?php $a = 1; $b = 1.2; $c = "abc"; echo gettype($a)."<br>"; echo gettype($b)."<br>"; echo gettype($c)."<br>"; ?> integerdoublestring

  43. Set type <?php$foo = "5bar"; // string$bar = true;   // booleansettype($foo, "integer"); // $foo is now 5   (integer)settype($bar, "string");  // $bar is now "1" (string)?>

  44. Set type <?php $testString = “10.2abc”; // call function settype to convert variable // testString to different data types print( "$testString" ); settype( $testString, "double" ); print( " as a double is $testString <br />" ); print( "$testString" ); settype( $testString, "integer" ); print( " as an integer is $testString <br />" ); settype( $testString, "string" ); print( "Converting back to a string results in $testString <br /><br />" ); ?> 10.2abc as a double is 10.2 10.2 as an integer is 10 Converting back to a string results in 10 

  45. Casting Data type <?php $data = "98.6 degrees"; echo "Now using type casting instead: <br>"; echo "As a string - ".(string) $data ; echo "<br> As a double - ".(double) $data; echo "<br> As an integer - ".(integer) $data; ?> Now using type casting instead: As a string - 98.6 degreesAs a double - 98.6As an integer - 98

  46. Casting Data type <?php $data = "98.6 degrees"; echo "Now using type casting instead: <br>"; echo "As a string - ".(string) $data ; echo "<br> As a double - ".(double) $data; echo "<br> As an integer - ".(integer) $data; ?> $variable = (datatype) $variable or value

  47. Casting Data type <?php $a = “ 12.4 abc” echo (int) $a; echo (double) ($a); echo (float) ($a); echo (string) ($a); ?>

  48. PHP Operators • The assignment operator = is used to assign values to variables in PHP. • The arithmetic operator + is used to add values together in PHP. • Assignment operators • Syntactical shortcuts • Before being assigned values, variables have value undef • Constants • Named values • define function

  49. PHP Operators • Arithmetic Operators • Assignment Operators • Incrementing/Decrementing Operators • Comparison Operators • Logical Operators

  50. Arithmetic Operators

More Related