Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Programming Language

Similar presentations


Presentation on theme: "Web Programming Language"— Presentation transcript:

1 Web Programming Language
Chapter 4

2 Introducing PHP Setting Up a Web Server PHP Basic Syntax PHP Variables
PHP Operators PHP Flow Control PHP Form Validation

3 PHP “Hypertext Pre Processor”
Common Server-side language powering 80% of websites Core Part of WAMP stack (Windows Apache MySQL PHP) Or LAMP, or MAMP

4 Set Up A Webserver If you already have a webserver – great!
If not, set up your local machine as a host, by installing wampserver. Create a file called hello.php in your www folder <?php echo “Hello World” ?> Open it in a browser; localhost/hello.php or /hello.php

5 PHP Variables Begin with $
Named by any combination of letters, numbers, and _ $Name Case Sensitive $NAME, $Name, $name are different variables Can store different types of data Strings, Integers, Floats, Boolean, Array & Objects Weakly Typed You don’t need to specify which type of data the variable will store No need for declaration

6 PHP Variables Opening PHP Tag <?php $x = 5; $y = “3.5”; $z = $x + $y; echo "The number is $z<br>"; echo "The number is " . $z . "<br>"; ?> Assignment Addition Output Concatenation Closing PHP Tag

7 Arrays Creates an Indexed array where “John” has index 0, and Fred has index 3. <?php $students = array(“John”, “Bob”, “Steve”); $students[] = “Fred”; echo “The first student is “ . $students[0]; ?>

8 Associative Arrays The Index could be a string
<?php $ages = array(“John”=>”18”, “Bob”=>”19”, “Steve”=>”22”); echo “John is “ . $ages[“John”]; ?> Each element is a “Key=>Value” Pair

9 Multidimensional Arrays
A 2 dimensional array is just an Array of Arrays <?php $students = array(); $students[] = array("John", "84", "A"); $students[] = array("Bob", "65", "C+"); $students[] = array("Steve", "72", "B"); echo "The first student's grade is " . $students[0][2]; ?>

10 Strings Strings are important in PHP (we are dealing with text on the web of course!) Function Name Purpose explode() Splits a string into an array implode() Converts an array into a string lcfirst() Makes the first character of a string lowercase str_replace() Replaces specified characters in a string str_word_count() Returns the number of words in the string strcmp() Compares two strings strlen() Returns the length of a string strpos() Returns the position of the first occurrence of a string inside another string strtoupper() Converts a string to upper case strtolower() Converts a string to lower case substr() Returns a specified part of a string ucfirst() Makes the first character of a string uppercase

11 PHP Operators (Arithmetic)
Arithmetic Operators Purpose Example + Addition $x=1+2; - Subtraction $x=2-1; * Multiplication $x=1*2; / Division $x=2/1; % Modulus Returns the remainder from a division $x=2%1; ++ Increment (add one to the value) $x++; -- Decrement (minus one from the value) $x--;

12 PHP Operators (Assignment)
Assignment Operators Example Equivalent = $a = $b; += $a += $b; $a = $a + $b; -= $a -= $b; $a = $a – $b; *= $a *= $b; $a = $a * $b; /= $a /= $b; $a = $a / $b; %= $a %= $b; $a = $a % $b; .= $a .= $b $a = $a . $b;

13 PHP Operators (Logical)
Logical Operators Purpose == Equal to != Not equal to <>  Less than Greater than <= Less than or equal to >= Greater than or equal to && AND || OR ! Not

14 PHP Flow Control (IF) <?php if($x>20) { echo “x is greater than 20”; } elseif($x<10) { echo “x is smaller than 20”; } else { echo “x is between 10 and 20”; } ?>

15 PHP Flow Control (Switch)
<?php switch ($x) { case “login”: echo “Display the Login Page”; break; case “register”: echo “Display the Register Page”; break; default: echo “Display the Guest Page”; break; } ?>

16 PHP Flow Control (While)
<?php $x = 10; while($x>0) { echo “There were “ . $x . “ in the bed, till someone said, roll over…<br>”; $x--; } ?>

17 PHP Flow Control (Do…While)
<?php $x = 10; do { echo “There were “ . $x . “ green apples, ready to be eaten…<br>”; $x--; } while($x>0); ?>

18 PHP Flow Control (For) <?php for($i = 0; $i<10; $i++) { echo “I have “ . $i . “ doors, but need 1 more…<br>”; } ?>

19 PHP Flow Control (Foreach)
<?php $count = 1; foreach($students as $name) { echo $count . “) Student name:- “ . $name . “<br>”; $count++; } ?>

20 PHP Flow Control (Foreach) Associative
<?php $count = 1; foreach($students as $name => $grade) { echo $count . “) Student “ . $name . “ got grade “ . $grade . “<br>”; $count++; } ?>

21 PHP Form Validation HTML’s <form> tag is used to create a form that can send data to the server. <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br /> <input type="submit" value="Go!"> </form>

22 $_POST The previous form sends data by the POST method to welcome.php
<body> Welcome <?php echo $_POST[“name”]; ?> </body>

23 $_GET An alternative is to send the data via the GET method
<form action="welcome.php" method="get"> Name: <input type="text" name="name"><br /> <input type="submit" value="Go!"> </form> <body> Welcome <?php echo $_GET[“name”]; ?> </body> What is the difference?

24 $_POST or $_GET Both arrays are Superglobals, accessible from any function, class or file on the page. Both create an array of key=>value pairs with the names of the form inputs and the values submitted The GET method passes variables via URL parameters, while POST passes it via HTTP Post method. The GET method is visible, so should not be used for sensitive data, but because it is visible, it can be bookmarked. However the URL is limited to around 2,000 characters.

25 print_r() A useful function for making a human readable version of an array <body> <?php print_r($_POST); ?> </body>

26 Simple Form Validation
<body> <?php $name = $_POST["name"]; if(strlen($name)<3) { echo "Name must be at least 3 characters."; } ?> </body>

27 Key Points PHP is an important Server Side programming language.
Before running PHP code a web server needs to be set up to process PHP code. When a web server receives a request for PHP, first it processes the code before returning HTML. Variables in PHP are weakly or loosely typed and can also be used to store arrays. A form can be used to send data to the server using the post method. Data sent via the post method then exists within the $_POST superglobal array, or using the get method to the $_GET superglobal array. The main difference between post and get is that when using get, the parameters are attached to the url, so that it can be bookmarked.


Download ppt "Web Programming Language"

Similar presentations


Ads by Google