Julian Springer Room 42 Joe Slovo. W3 schools recommend that you have a basic understanding of HTML...

41
Julian Springer Room 42 Joe Slovo

Transcript of Julian Springer Room 42 Joe Slovo. W3 schools recommend that you have a basic understanding of HTML...

Page 1: 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.

Julian SpringerRoom 42Joe Slovo

Page 2: 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.

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

Page 3: 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.

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.

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

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.

Page 5: 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.

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 <?php and ends with ?>

Each line must end with a semicolon; Now for some tradition...

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

<html><body>

<?phpecho "Hello World";?>

</body></html>

Yes, this sends ‘Hello World’ to the browser.

PHP can be placed anywhere within the document, as the need arises.

Page 7: 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.

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

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

Commenting is once again in the same format as Java

// for a single line

And /*for *multiple *lines*/

Page 9: 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.

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”;

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

More examples:

<?php$txt="Hello World!";$x=16;?>

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.

Page 11: 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.

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.

Page 12: 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.

Strings are used for character values.

Strings can be manipulated once we have them as variables. For example:

<?php$txt="Hello World";echo $txt;?>

Will once again print ‘Hello World’

Page 13: 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.

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: <?php

$txt1="Hello World!";$txt2="What a nice day!";echo $txt1 . " " . $txt2;?>

Will combine the two strings.

Page 14: 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.

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:

Page 15: 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.

<?phpecho strlen("Hello world!");?>

Prints out an integer value of 12.

<?php$index = strpos("Hello world!","world");?>

This assigns the position of the start of the string world to the variable index.

Page 16: 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.

Operator Description Example Result

+ Addition x=2x+2

4

- Subtraction x=25-x

3

* Multiplication x=4x*5

20

/ Division 15/55/2

32.5

% Modulus (division remainder) 5%210%810%2

120

++ Increment x=5x++

x=6

-- Decrement x=5x--

x=4

Page 17: 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.

Operator

Example Is The Same As

= x=y x=y

+= x+=y x=x+y

-= x-=y x=x-y

*= x*=y x=x*y

/= x/=y x=x/y

.= x.=y x=x.y

%= x%=y x=x%y

Page 18: 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.

Operator

Description Example

== is equal to 5==8 returns false

!= is not equal 5!=8 returns true

<> is not equal 5<>8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to 5>=8 returns false

<= is less than or equal to 5<=8 returns true

Page 19: 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.

Operator

Description Example

&& and x=6y=3 (x < 10 && y > 1) returns true

|| or x=6y=3 (x==5 || y==5) returns false

! not x=6y=3 !(x==y) returns true

Page 20: 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.

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.

Page 21: 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.

<html><body>

<?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!";?>

</body></html>

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

Page 22: 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.

<html><body>

<?php$d=date("D");if ($d=="Fri")  echo "Have a nice weekend!";else  echo "Have a nice day!";?>

</body></html>

Either it’s Friday, or another day.

Page 23: 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.

<html><body>

<?php$d=date("D");if ($d=="Fri")  {  echo "Hello!<br />";  echo "Have a nice weekend!";  echo "See you on Monday!";  }?>

</body></html>

Note those curly braces

Page 24: 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.

<html><body>

<?php$d=date("D");if ($d=="Fri")  echo "Have a nice weekend!";elseif ($d=="Sun")  echo "Have a nice Sunday!";else  echo "Have a nice day!";?>

</body></html>

Elseif works for multiple cases

Page 25: 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.

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

Page 26: 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.

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

Page 27: 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.

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? <?php

$ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";?>

Will output Peter is 32 years old. See how Peter is used as an ID key

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

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.

Page 29: 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.

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

Page 30: 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.

What does a while look like? <html>

<body>

<?php$i=1;while($i<=5)  {  echo "The number is " . $i . "<br />";  $i++;  }?>

</body></html>

Much the same as Java.

Page 31: 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.

Do while <html>

<body>

<?php$i=1;do  {  $i++;  echo "The number is " . $i . "<br />";  }while ($i<=5);?>

</body></html>

Always executes once.

Page 32: 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.

For loops <html>

<body>

<?phpfor ($i=1; $i<=5; $i++)  {  echo "The number is " . $i . "<br />";  }?>

</body></html>

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.

Page 33: 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.

For each <html>

<body>

<?php$x=array("one","two","three");foreach ($x as $value)  {  echo $value . "<br />";  }?>

</body></html>

For every loop iteration, the value of the current array element is assigned to value. Then the pointer moves on.

Page 34: 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.

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.

Page 35: 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.

<html><body>

<?phpfunction writeName(){echo "Kai Jim Refsnes";}

echo "My name is ";writeName();?>

</body></html>

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.

Page 36: 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.

What i mean by form, is an actual form. The following form attempts to get data from the user.

<html><body>

<form action="welcome.php" method="post">Name: <input type="text" name="fname" />Age: <input type="text" name="age" /><input type="submit" /></form>

</body></html>

See how it calls welcome.php?

Page 37: 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.

So what is Welcome.php? <html>

<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />You are <?php echo $_POST["age"]; ?> years old.

</body></html>

Page 38: 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.

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.

Page 39: 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.

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

Page 40: 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.

$_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.

Page 41: 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.

The rest of the functions are documented on the site www.w3schools.com under the advanced PHP section.

Thanks you all you’ve been a wonderful audience, may you have many children and your hair not fall out. :D