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

Post on 04-Jan-2016

215 views 0 download

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

Julian SpringerRoom 42Joe Slovo

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

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.

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.

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

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

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

Commenting is once again in the same format as Java

// for a single line

And /*for *multiple *lines*/

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

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.

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.

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’

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.

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:

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

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

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

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

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

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.

<html><body>

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

</body></html>

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

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

<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

<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

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

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

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

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.

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

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.

Do while <html>

<body>

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

</body></html>

Always executes once.

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.

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.

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.

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

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?

So what is Welcome.php? <html>

<body>

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

</body></html>

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.

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

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

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