PHP 03: What is PHP?

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:

$_POST['variable'];

String Manipulation

Here is a list of some valuable string manipulators:

trim() // takes out any white spaces on both sides
strlen() // counts the length of the string
strtolower() // sets the whole string to lowercase
strtoupper() // sets the whole string to uppercase
ucwords() // sets the whole string to Title Case

 

CONCATENATION

$var1 . $var2
$var1, $var2
$sql = "SELECT * ";
$sql .= "FROM table1 ";
$sql .= "WHERE user = 1";

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

echo "Hello, " . $name . " how are you today?";

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

echo "Hello, {$name} how are you today?";

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

  1. 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.
  2. Your form is going to need one textbox to capture a string and two textboxes to capture two numbers.
  3. Here are the requirements for the form:
    1. Take a word and output it as all lower case.
    2. Take the same word and output it as all uppercase.
    3. Count the number of letters in the word, and output the second to last letter.
    4. Uppercase each word in a sentence.
    5. Take two integers and output it by multiplying it together.
    6. Subtract the two numbers.

Includes (four types)

include( )

If you want to include other code from other php files, you use the include call.

include('myscript.php');

If the script is not in the current directory you must include the path along with the filename.

include('pathtofolder/myscript.php')

 


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.

Written by