Commenting
// – single line comment
/*
multi-line comment
*/
Quotes
Single quotes will print exactly as seen, not caring about $.
Double quotes will print recognizing variables starting with $.
PHP requires a Server (Server side programming language)
Since PHP is a server-based language, all the compilations happen at the server level. You will need to run “server” software on your local machine if you wish to test your PHP pages locally.
Moving Between Forms
There are two methods used when passing data between forms. The first is the POST method. This method is more secure since the data can’t easily be seen in the URL like the GET method.
The second method is the GET method. This method sends the form data through the URL.
HTML FORM
SECOND PAGE – PHP
Sometimes, it is best to process the form data right on the same page. To do this you would simply check to see if the form has “POSTED” yet. If it has, you would gather the form data using the operation:
[code]$_POST[‘variable’];[/code]
String Manipulation
Here is a list of some valuable string manipulators:
[code]trim() // takes out any white spaces on both sides[/code]
[code]strlen() // counts the length of the string[/code]
[code]strtolower() // sets the whole string to lowercase[/code]
[code]strtoupper() // sets the whole string to uppercase[/code]
[code]ucwords() // sets the whole string to Title Case[/code]
CONCATENATION
[code]$var1 . $var2[/code]
[code]$var1, $var2[/code]
[code]$sql = "SELECT * ";
$sql .= "FROM table1 ";
$sql .= "WHERE user = 1";[/code]
Things to note:
1) notice the use of the .= this is used to append the first $sql to the second.
2) notice also the spacing after the * on the first row. If you are not careful, your sql statements will not run correctly if it is not properly formatted.
OUTPUTTING A STRING WITH PHP VARIABLES
[code]echo "Hello, " . $name . " how are you today?";[/code]
Things to note:
1) echo is used to output to screen.
2) using spaces after the comma and before how, allow for proper spacing after using a variable.
3) always end your php code with a semicolon.
ALTERNATIVE WAY TO OUTPUT A STRING WITH PHP VARIABLES
[code]echo "Hello, {$name} how are you today?";[/code]
Things to note:
1) much easier to use the curly braces without all the dots for combining the strings with variables.
Numbers
PHP supports the standard arithmetic operators like other programming languages. The same rules of order of operations also apply. As far as data types go, the most common are the:
string – text or numbers. You can’t add two strings together.
integer – whole numbers
float (also called double) – numbers with decimals
boolean – (yes / no) or (true / false)
array – a list of things (covered in later pages)
Practice
- Create two pages (first an html page called form.html and the other called second.php) You are going to be filling out a form and sending the data to the second php page to be manipulated.
- Your form is going to need one textbox to capture a string and two textboxes to capture two numbers.
- Here are the requirements for the form:
- Take a word and output it as all lower case.
- Take the same word and output it as all uppercase.
- Count the number of letters in the word, and output the second to last letter.
- Uppercase each word in a sentence.
- Take two integers and output it by multiplying it together.
- Subtract the two numbers.
Includes (four types)
include( )
If you want to include other code from other php files, you use the include call.
[code]include(‘myscript.php’);[/code]
If the script is not in the current directory you must include the path along with the filename.
[code]include(‘pathtofolder/myscript.php’)[/code]
require( )
When you issue an include() call, if the file is not found the system will not issue an error. If you use require, and the file to include is not found, an error will be issued.
include_once( ) or require_once( )
If you only want to include or require the file once, simply use include_once or require_once and if the file has already been used, it will not be loaded a second time.