1 / 80

Introduction to php

Introduction to php. PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP, Perl, JSP. What you mean by hypertext Pre-processor?.

caver
Download Presentation

Introduction to 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. Introduction to php

  2. PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. • PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP, Perl, JSP

  3. What you mean by hypertext Pre-processor? • php is called hypertext pre-processor as its libraries are already compiled. • when any person request for any php page in the address bar of the browser that request is first sent to the server then server interpret php files and return back response in form of Html

  4. Scripting Languages A ‘script‛ is a collection of program or sequence of instructions that is interpreted or carried out by another program rather than by the computer processor. Two types of scripting languages are • Client-side Scripting Language • Server-side Scripting Language • In server-side scripting, (such as PHP, ASP) the script is processed by the server Like: Apache, ColdFusion, ISAPI and Microsoft's IIS on Windows. • Client-side scripting such as JavaScript runs on the web browser.

  5. What you mean by server side scripting??

  6. What you mean by client side scripting??

  7. What is PHP? • PHP: Hypertext Pre-processor (earlier called, Personal Home Page) PHP is an HTML-embedded, • PHP is a server-side scripting language designed for web development. and a powerful tool for making dynamic and interactive Web pages • It is also used as a general purpose programming language. It was created by Rasmus Lerdorf in 1994 and appeared in the market in 1995. • PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. • PHP source code and the HTML output of the code • It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. • PHP Syntax based on Perl, Java, and C

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

  9. Common uses of PHP • PHP can generate dynamic page content • PHP can create, open, read, write, delete, 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 be used to control user-access • PHP can encrypt data

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

  11. How does PHP work?

  12. PHP - Environment Setup In order to develop and run PHP Web pages three vital components need to be installed on your computer system. • Web Server − PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server. Download Apache for free here − https://httpd.apache.org/download.cgi • Database − PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. Download MySQL for free here − https://www.mysql.com/downloads/ • PHP Parser− In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser.

  13. Set Up PHP on Your Own PC : Method 1 • Install a web server • install PHP • install a database, such as MySQL • installation instructions for PHP: http://php.net/manual/en/install.php

  14. Set Up PHP on Your Own PC : Method 2 Installing XAMPP. • XAMPP is the most popular PHP development environment • XAMPP is a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl. The XAMPP open source package has been set up to be incredibly easy to install and to use. • Acronym for • X (any Operating System), • Apache (Web server), • MySQL Database or Maria Database, • PHP Language • PERL https://www.apachefriends.org/index.html

  15. Set Up PHP on Your Own PC : Method 3 Installing WAMP. • WampServer is a Windows web development environment. • It allows you to create web applications with Apache2, PHP and a MySQL database. Alongside, PhpMyAdmin allows you to manage easily your databases. • Acronym for • Windows Operating System • Apache(Web server) • MySQL Database • PHP Language

  16. Creating Your Development Environment • Consider, every thing required for php script execution is installed. How to create the PHP file. • Use text editor to write the PHP script and save the file with the extension of .php Example (resume.php) List of some suggested editors: • Vi • Emacs • Pico • Macintosh’s BBEdit or SimpleText • Windows Notepad • Wordpad • Sublime Text ( Recommended) The text editor needs to be able to save files in Plain Text Format (Text without any special formatting codes)

  17. IDEto create PHP file We can use the IDE to create PHP file. • Komoda • Maguma • PHPEdit • Zend Studio • Most of them will cost money, although they have some good features for php development.

  18. Creating First PHP Page • A PHP script can be placed anywhere in the document. • A PHP script starts with <?php and ends with ?> • PHP statements end with a semicolon (;). <?php ……………………………………………… ……………………... ?>

  19. Example <!DOCTYPE html><html><body><h1>My first PHP page</h1><?phpecho "Hello World!";?></body></html>

  20. Running Your First PHP Page • Make sure web server is running • Deploy your php project into the htdocsfolder(If XAMPP) / wwwfolder (If WAMP) • Access your php file in browser using below syntax. localhost:/project folder name/filename.php

  21. Mixing HTML with PHP • One of the charms of PHP is that, we can intersperse our PHP code with HTML. • HTML will be displayed by the browser, PHP will be run on your server – and if that PHP generates some HTML, that HTML will be displayed in the browser as well.

  22. Only HTML HTML with PHP <html> <head> <title> Using PHP and HTML together </title> </head> <body> <h1> Using PHP and HTML together </h1> </body> </html> <html> <head> <title> Using PHP and HTML together </title> </head> <body> <h1> Using PHP and HTML together </h1> Here is your PHP info: <?php phpinfo(); ?> </body> </html>

  23. Comments in PHP • A commentis the portion of a program that exists only for the human reader and stripped out before displaying the programs result.  • Single-line comments − They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments • <? // This is a single-line comment #This is also a single-line comment ?>Multi-lines printing− Here are the examples to print multiple lines in a single print statement  • <? /* This is a multiple-lines comment block • that spans over multiplelines*/ ?>

  24. PHP  Variables • Variables are "containers" for storing information. • A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). • 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) • Remember that PHP variable names are case-sensitive!

  25. Creating (Declaring) PHP Variables • In PHP, a variable starts with the $ sign, followed by the name of the variable: • Example: • <?php$txt = "Hello world!";$x = 5;$y = 10.5;?>

  26. Output Variables • The PHP echo statement is often used to output data to the screen. • The following example will show how to output text and a variable: <!DOCTYPE html><html><body><?php$x = 5;$y = 4;echo $x + $y;?></body></html> <!DOCTYPE html><html><body><?php$txt = "W3Schools.com";echo "I love $txt!";?></body></html>

  27. PHP is a Loosely Typed Language • In PHP we did not have to tell which data type the variable is. • PHP automatically converts the variable to the correct data type, depending on its value. • In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.

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

  29. Global and Local 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: <!DOCTYPE html><html><body><?phpfunctionmyTest() {    $x = 5; // local scope  echo "<p>Variable x inside function is: $x</p>";} myTest();// using x outside the function will generate an errorecho "<p>Variable x outside function is: $x</p>";?></body></html> • <!DOCTYPE html><html><body><?php$x = 5; // global scopefunctionmyTest() {    // using x inside this function will generate an errorecho "<p>Variable x inside function is: $x</p>";} myTest();echo "<p>Variable x outside function is: $x</p>";?></body></html>

  30. PHP The global Keyword • The global keyword is used to access a global variable from within a function. • To do this, use the global keyword before the variables (inside the function): • <!DOCTYPE html><html><body><?php$x = 5;$y = 10;functionmyTest() {    global $x, $y;    $y = $x + $y;} myTest();  // run functionecho $y; // output the new value for variable $y?></body></html>

  31. $GLOBALS[index] • PHP also stores all global variables in an array called $GLOBALS[index]. • The index holds the name of the variable. • This array is also accessible from within functions and can be used to update global variables directly. <?php$x = 5;$y = 10;functionmyTest(){$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];} myTest();echo $y; // outputs 15?>

  32. 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. • To do this, use the static keyword when you first declare the variable: <?phpfunctionmyTest() { static $x = 0;echo $x;    $x++;}myTest();myTest();myTest();?>

  33. PHP echo and print Statements • In PHP there are two basic ways to get output: echo and print. • echo and print are more or less the same. They are both used to output data to the screen. Difference between echo and print • echo has no return value while print has a return value of 1 so it can be used in expressions. • echo can take multiple parameters (although such usage is rare) while print can take one argument. • echo is marginally faster than print.

  34. PHP echo Statement • The echo statement can be used with or without parentheses: echo or echo(). Display Text • The following example shows how to output text with the echo command (notice that the text can contain HTML markup): <?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.";?>

  35. Display Variables • The following example shows how to output text and variables with the echo statement: <?php$txt1 = "Learn PHP";$txt2 = "W3Schools.com";$x = 5;$y = 4;echo "<h2>" . $txt1 . "</h2>";echo "Study PHP at " . $txt2 . "<br>";echo $x + $y;?>

  36. PHP print Statement • The print statement can be used with or without parentheses: print or print(). Display Text • The following example shows how to output text with the print command (notice that the text can contain HTML markup): <?phpprint "<h2>PHP is Fun!</h2>";print "Hello world!<br>";print "I'm about to learn PHP!";?>

  37. Display Variables • The following example shows how to output text and variables with the print statement: <?php$txt1 = "Learn PHP";$txt2 = "W3Schools.com";$x = 5;$y = 4;print "<h2>" . $txt1 . "</h2>";print "Study PHP at " . $txt2 . "<br>";print $x + $y;?>

  38. PHP Data Types • Variables can store data of different types, and different data types can do different things. • PHP supports the following data types: • String • Integer • Float (floating point numbers - also called double) • Boolean • Array • Object • NULL

  39. PHP String • A string is a sequence of characters, like "Hello world!". • A string can be any text inside quotes. You can use single or double quotes: <?php$x = "Helloworld!";$y = 'Helloworld!';echo $x;echo "<br>"; echo $y; ?>

  40. PHP Integer • An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. • Rules for integers: • An integer must have at least one digit • 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) • In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

  41. PHP Integer <?php$x = 5985;var_dump($x);?>

  42. PHP Float • A float (floating point number) is a number with a decimal point or a number in exponential form. • In the following example $x is a float. The PHP var_dump() function returns the data type and value: <?php$x = 10.365;var_dump($x);?>

  43. PHP Boolean • A Boolean represents two possible states: TRUE or FALSE. • Booleans are often used in conditional testing. $x = true;$y = false;

  44. PHP Array • An array stores multiple values in one single variable. • In the following example $cars is an array. The PHP var_dump() function returns the data type and value: <?php$cars = array("Volvo","BMW","Toyota");var_dump($cars);?>

  45. PHP Object • 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: • <?phpclass Car { function Car() {        $this->model = "VW";    }}// create an object$herbie = new Car(); • // show object propertiesecho $herbie->model;?>

  46. PHP NULL Value • Null is a special data type which can have only one value: NULL. • A variable of data type NULL is a variable that has no value assigned to it. Tip: If a variable is created without a value, it is automatically assigned a value of NULL. Variables can also be emptied by setting the value to NULL: <?php$x = "Hello world!";$x = null;var_dump($x);?>

  47. PHP Operators • Operators are used to perform operations on variables and values • PHP divides the operators in the following group: • Arithmetic operators • Assignment operators • Comparison operators • Increment/Decrement operators • Logical operators • String operators • Array operators

  48. PHP Arithmetic Operators • The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. • Addition : $x + $y • <html><body><?php$x = 10;  $y = 6;echo $x + $y;?>  </body></html>

  49. PHP Arithmetic Operators • Subtraction :$x - $y <html><body><?php$x = 10;  $y = 6;echo $x - $y;?>  </body></html>

  50. PHP Arithmetic Operators • Multiplication : $x * $y <html><body><?php$x = 10;  $y = 6;echo $x * $y;?>  </body></html>

More Related