Presentation is loading. Please wait.

Presentation is loading. Please wait.

Julian Springer Room 42 Joe Slovo.  W3 schools recommend that you have a basic understanding of HTML and of JavaScript before attempting to grasp PHP.

Similar presentations


Presentation on theme: "Julian Springer Room 42 Joe Slovo.  W3 schools recommend that you have a basic understanding of HTML and of JavaScript before attempting to grasp PHP."— Presentation transcript:

1 Julian Springer Room 42 Joe Slovo

2  W3 schools recommend that you have a basic understanding of HTML and of JavaScript before attempting to grasp PHP.  They provide comprehensive tutorials on these on their site. You can do this for homework

3  It stands for Hypertext Preprocessor  It is a server side scripting language. This means that the scripts are run on the server itself as opposed to the client machine  It supports many databases like MySQL, Oracle, etc.  If you combine PHP with MySQL you create a cross platform system.

4  It runs on everything. Windows, Linux, Mac... Nah just jokes, it won’t run on Mac, but then, who does?  It’s easy to install! If you have a PHP supported server, it’s already there!  You just need to create some PHP files and the server will parse them for you.  It’s also free.

5  PHP is a script language, which means it is a quick way to send data to the browser. It is normally added in to already existing HTML code.  Any block of PHP always starts with  Each line must end with a semicolon;  Now for some tradition...

6   Yes, this sends ‘Hello World’ to the browser.  PHP can be placed anywhere within the document, as the need arises.

7  There are 2 statements to output text in PHP:  Echo  Print  Statements will only run if the file has a.php extension. .html will ignore it

8  Commenting is once again in the same format as Java  // for a single line  And /*for  *multiple  *lines*/

9  Variables are used to store values such as numbers, text or arrays (yes,there are arrays!)  Variables can be reused as much as you want during your script.  All variables start with a $ hence  $name = “Caesar”;

10  More examples:   This stores Hello World in the variable txt and 16 into x.  Notice how you do not need to declare data types. PHP is smart enough to figure it out.  Also note that no declaration is required.

11  Variable names must start with either a letter or an underscore.  It can only contain alpha-numeric characters and underscores.  Trying to have variable names with spaces in will break your script.

12  Strings are used for character values.  Strings can be manipulated once we have them as variables. For example:   Will once again print ‘Hello World’

13  This is basically all we can do with strings.  Concatenation is done with the. Operator. (yes that’s a full stop)  An example of this:   Will combine the two strings.

14  We can also get the length of a String, using the strlen(“some string”) function. It returns an integer value of how many characters there are.  The strpos() function allows us to search for the position of a string within a string. It returns an integer, which is the index of the string.  The counting starts at 0, as usual.  Some examples:

15   Prints out an integer value of 12.   This assigns the position of the start of the string world to the variable index.

16 OperatorDescriptionExampleResult +Additionx=2 x+2 4 -Subtractionx=2 5-x 3 *Multiplicationx=4 x*5 20 /Division15/5 5/2 3 2.5 %Modulus (division remainder)5%2 10%8 10%2 120120 ++Incrementx=5 x++ x=6 --Decrementx=5 x-- x=4

17 OperatorExampleIs The Same As =x=y +=x+=yx=x+y -=x-=yx=x-y *=x*=yx=x*y /=x/=yx=x/y.=x.=yx=x.y %=x%=yx=x%y

18 OperatorDescriptionExample ==is equal to5==8 returns false !=is not equal5!=8 returns true <>is not equal5<>8 returns true >is greater than5>8 returns false <is less than5<8 returns true >=is greater than or equal to5>=8 returns false <=is less than or equal to5<=8 returns true

19 OperatorDescriptionExample &&andx=6 y=3 (x 1) returns true ||orx=6 y=3 (x==5 || y==5) returns false !notx=6 y=3 !(x==y) returns true

20  If (basically just a check)  If.. Else..(the same as normal)  There is an else if. If you need to test multiple cases.  Switch (just for Jess) there is a case switch option also.

21   If it is Friday, have a nice weekend...

22   Either it’s Friday, or another day.

23  "; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?>  Note those curly braces

24   Elseif works for multiple cases

25  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; }  Same as Java

26  Array declaration is very simple in PHP  $cars=array("Saab","Volvo","BMW","Toyota"); is an array of strings, assigned numerically starting at 0.  $cars[0] will give us Saab, etc...  This is a numeric array

27  There is also an associative arrays. Which relies on an ID key.  $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); is how it would be declared.  But how does it work?   Will output Peter is 32 years old. See how Peter is used as an ID key

28  But what about multidimensionals? I hear you cry!  $families = array ( "Griffin"=>array  ( "Peter", "Lois", "Megan“,  “Chris”,  “Stewie” ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) );  Note the double declaration. We call it using $families[‘Griffin’][2], to call Meg for example.

29  The standard loops all exist in PHP.  While  Do...while  For  Foreach (this may be a new one for you!)

30  What does a while look like?  "; $i++; } ?>  Much the same as Java.

31  Do while  "; } while ($i  Always executes once.

32  For loops  "; } ?>  Interesting to note, where we have variable i, we can use any code that must be executed once at the start. Likewise with the increment.

33  For each  "; } ?>  For every loop iteration, the value of the current array element is assigned to value. Then the pointer moves on.

34  PHP’s strength is it’s built in functions. As there are over 700 of them...  A function is what we would call a method in Java.  function functionName () { code to be executed ; }  The same naming applies.  Adding parameters is the same as in Java.

35   Notice how there is no void keyword. Yet it returns nothing. And the compiler will not complain. To return, you simply add the keyword return and the variable you wish to return.

36  What i mean by form, is an actual form. The following form attempts to get data from the user.  Name: Age:  See how it calls welcome.php?

37  So what is Welcome.php?  Welcome ! You are years old.

38  If you are going to use a form on your webpage, you should really consider checking that your user isn’t completely stupid and has entered his age as his name or something.  A good way to do this on the server is to post the form to itself, hence not navigating away from the page, but showing where the error has occurred.

39  The function $_GET is used to collect values sent with the method get.  This is used when a user wants to submit data to the server, we can access the data directly as it comes in. For example, a welcome screen.  When to use method=“get” in HTML:  This shows the data in the URL, which is a safety risk. However it makes it easier to bookmark the page.  Do not use for large variables. <2000 chars plz

40  $_POST, is similar to the get method we have just seen.  This is a more secure method as the info is invisible to others and has no limits. It will not show in the URL.  It is however impossible to bookmark such a page.  There is also a $_REQUEST function that contains the contents of both the get and post.

41  The rest of the functions are documented on the site www.w3schools.com under the advanced PHP section.www.w3schools.com  Thanks you all you’ve been a wonderful audience, may you have many children and your hair not fall out. :D


Download ppt "Julian Springer Room 42 Joe Slovo.  W3 schools recommend that you have a basic understanding of HTML and of JavaScript before attempting to grasp PHP."

Similar presentations


Ads by Google