acm web development workshop - php

25
ACM Web Development Workshop - PHP By Luis Torres

Upload: ely

Post on 15-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

ACM Web Development Workshop - PHP. By Luis Torres. DON’T FORGET TO INSTALL “WAMP or MAMP SERVER” ON YOUR COMPUTER google /yahoo/ bing it . Running wamp :. Click Start the services!. And put online!. Localhost will open whatever is named index.html/ index.php in the www directory. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ACM Web Development Workshop - PHP

ACM Web Development Workshop - PHP

By Luis Torres

Page 2: ACM Web Development Workshop - PHP

DON’T FORGET TO INSTALL “WAMP or MAMP SERVER” ON YOUR COMPUTER

google/yahoo/bing it

Page 3: ACM Web Development Workshop - PHP

Running wamp:

Page 4: ACM Web Development Workshop - PHP

Click Start the services!And put online!

Page 5: ACM Web Development Workshop - PHP

Localhost will open whatever is named index.html/index.php in the www directory

Page 6: ACM Web Development Workshop - PHP

This folder will contain our website files, if you want to run a php file it has to be INSIDE of this folder

Page 7: ACM Web Development Workshop - PHP

This is how the www folder will look like.(more or less)

Page 8: ACM Web Development Workshop - PHP

Open “index.php”, erase all its content and replace with the following

BE CAREFUL:This is the concatenation character in PHP.In java is the “+” character. Don’t confuse them!

Page 9: ACM Web Development Workshop - PHP

Different approaches to code in php

Page 10: ACM Web Development Workshop - PHP

Lets do functions!

Our function doMath will take two inputs, and compute its sum.

Since we are not declaring types, we ASSUME that the inputs will be integers.

Inside the function we could check the types if we wanted to make sure that everything is ok.

Page 11: ACM Web Development Workshop - PHP
Page 12: ACM Web Development Workshop - PHP

OMG PHP is easy :D

Well.. Lets continue to the other stuff

Page 13: ACM Web Development Workshop - PHP

PHP $_GET Purpose: Sometimes we want the same page

to have a function that can do multiple outputs based on the input.

Ex: google search will work the same way, but will yield different outcomes.

Page 14: ACM Web Development Workshop - PHP

PHP $_GET How does it works? In the google example, they use the URL, to

pass variables that will be used in that page.

Page 15: ACM Web Development Workshop - PHP

Wait… What? Lets go back to the doMath function. For the purpose of teaching, lets assume you

have this URL: (you hard code the rest of the url)

Lets break it down:localhost/yourFile.php <- this is your file

? Question mark shows where variables begin variableA <- the variable “varaibleA” has a

value of 2 & <- this character concatenates multiple

variables variableB <- the variable “varaibleB” has a

value of 3

Page 16: ACM Web Development Workshop - PHP

Notice how you get the values using get.GET will always get values from the URL

Page 17: ACM Web Development Workshop - PHP

But without the URL it wont work :’( Here is how to fix that, just check if the $_GET

“isset()”:<html><body><?php if(isset($_GET['variableA'])) {

$a=$_GET['variableA']; } else { echo "no input in URL, using variableA as 2<br>"; $a=2; } if(isset($_GET['variableB'])) {

$b=$_GET['variableB']; } else { echo "no input in URL, using variableB as 3<br>"; $b=3; } echo doMath($a,$b);?></body><?phpfunction doMath($a,$b){ return $a+$b;}

Page 18: ACM Web Development Workshop - PHP

Now it works for both

Page 19: ACM Web Development Workshop - PHP

But, how can I change the values being passed without hardcoding? We have Forms

<html><body><form action=“yourFile.php" method="get">variable A: <input type="text" name="variableA" />variable B: <input type="text" name="variableB" /><input type="submit" /></form><?php

Rest of the code from previous example…?>

Page 20: ACM Web Development Workshop - PHP

How does it work?

<html><body><form action=“yourFile.php" method="get">variable A: <input type="text" name="variableA" />variable B: <input type="text" name="variableB" /><input type="submit" /></form><?php

Rest of the code from previous example…?>

Input type submit is a button, after clicking the button it will go to the specified file, with the specified values and execute.

Will be obtained in yourFile.php using $_GET[‘variableName’]

Page 21: ACM Web Development Workshop - PHP

What if I want to be mysterious and not show anything in the URL? You can use the POST function Similar to $_GET[‘variable’], you use

$_POST[‘variable’]<form action=“yourFile.php" method=“post">variable A: <input type="text" name="variableA" />variable B: <input type="text" name="variableB" /><input type="submit" />……<?php if(isset($_POST['variableA'])) {

$a=$_POST['variableA']; }…….You get the idea, it will work, and wont show the variables in the URL

Page 22: ACM Web Development Workshop - PHP

Are they different?GET-variables shown at all times… horrible if you use get to pass a password

-since it is shown in the URL, you can bookmark a specific location on your browser. Cool now you can send a specific query of a website to a friend.

-URLs have a size limit!2048 characters

POST-variables hidden… you want to use this when submitting a password

-cant be bookmarked, useful if you don’t want users to return to a specific part of a site…like submitting the payment. Since post variables don’t stay there, when the code tries to execute it will fail because of the missing variables.

-don’t have a limit, and they are encrypted

Page 23: ACM Web Development Workshop - PHP

PHP Include What if you have a multiple functions, and you

don’t want to copy paste them on all your files.

Really simple:

Page 24: ACM Web Development Workshop - PHP

PHP Include WONT WORK if you put the include after you

method call. Make sure all your includes are at the top of

your code if possible.

Page 25: ACM Web Development Workshop - PHP

Ermm we got this far..? We are missing: forms with PHP (dynamic forms) Sessions SQL/databases Javascript Ajax

So basically another workshop or 2..or more.