Learn PHP 7 to 12

Posted by Webmaster |

Lesson 7 – Arrays

Remember to end every instruction with a semicolon ;

Special note. In order to display bits of code additional spaces have been inserted into some commands so they render as text. Look at the source code for the real stuff.

Arrays have indexes and values. Indexes can be numeric or string (Associative Arrays).

See http://www.php.net/ref.array

Arrays can be filled and then printed for debugging – print_r(Array)

Array
(
    [0] => 2.5
    [1] => 2.1
    [2] => 2.6
    [3] => 3.4
    [4] => 1.8
    [5] => 2
    [6] => 1.7
    [7] => 1
    [8] => 1.9
    [9] => 2.1
    [10] => 3.4
    [11] => 2
)

Or you can print single values -echo $rain[3] . (the array is called $rain)
3.4

You can cycle through all the values like this – while (list($month, $value) = each($rain)) { do something }
In month 0 there were 2.5 inches of rain.
In month 1 there were 2.1 inches of rain.
In month 2 there were 2.6 inches of rain.
In month 3 there were 3.4 inches of rain.
In month 4 there were 1.8 inches of rain.
In month 5 there were 2 inches of rain.
In month 6 there were 1.7 inches of rain.
In month 7 there were 1 inches of rain.
In month 8 there were 1.9 inches of rain.
In month 9 there were 2.1 inches of rain.
In month 10 there were 3.4 inches of rain.
In month 11 there were 2 inches of rain.

or like this – foreach ($rain as $month => $value) { do something }
In month 0 there were 2.5 inches of rain.
In month 1 there were 2.1 inches of rain.
In month 2 there were 2.6 inches of rain.
In month 3 there were 3.4 inches of rain.
In month 4 there were 1.8 inches of rain.
In month 5 there were 2 inches of rain.
In month 6 there were 1.7 inches of rain.
In month 7 there were 1 inches of rain.
In month 8 there were 1.9 inches of rain.
In month 9 there were 2.1 inches of rain.
In month 10 there were 3.4 inches of rain.
In month 11 there were 2 inches of rain.

Associative Arrays

Arrays can be filled and then printed for debugging – print_r(Array)

Array
(
    [Jan] => 2.5
    [Feb] => 2.1
    [Mar] => 2.6
    [Oct] => 2.1
    [Nov] => 3.4
    [Dec] => 2
    [Jul] => 1.7
    [Aug] => 1
    [Sep] => 1.9
    [Apr] => 3.4
    [May] => 1.8
    [Jun] => 2
)

N.B. Array reported in the order in which the data was defined.

Sorting Arrays

Lots of caution here. Seem to be serious differences between 4 and 5 as most functions do nothing.

Before After
Jan = 2.5
Feb = 2.1
Mar = 2.6
Apr = 3.4
May = 1.8
Jun = 2
Jul = 1.7
Aug = 1
Sep = 1.9
Oct = 2.1
Nov = 3.4
Dec = 2
Apr = 3.4
Aug = 1
Dec = 2
Feb = 2.1
Jan = 2.5
Jul = 1.7
Jun = 2
Mar = 2.6
May = 1.8
Nov = 3.4
Oct = 2.1
Sep = 1.9

Lesson 8 – Regular Expressions

Simple

needle = “Ike”, haystack = “I like sailing”

ereg(“needle” , “haystack”) – returns TRUE if needle is in haystack. Case sensitive.

No Match

eregi(“needle” , “haystack”) – returns TRUE if needle is in haystack. Not case sensitive.

Match

Testing sets

ereg(“[A-Z]”,$haystack) will give TRUE if there is any capital letter in $haystack

Match

ereg(“[^A-Z]”,$haystack) (the ^ inside the [ is a NEGATIVE) will give TRUE if there is any non capital letter in $haystack

Match

To remove the need for defining common sets of characters the following classes are available.

Class Name Definition
alnum all upper and lower alpha chars and all numbers
alpha all letters A_Z 7 a-z
digit all digits 0 – 9
lower all letters a-z lower case
print all printable characters inc space
punct all punctuation, i.e. all print less all alnum and space
space all whitespace chars inc space, tab and new line.
upper all uppercase letters A – Z

Position in a string

The ‘^’ character indicates the first character, the ‘$’ indicates the last.

‘This phrase’ returns FALSE for – if (ereg(“^[a-z]”,”This phrase” )).
‘this phrase’ returns TRUE for – if (ereg(“^[a-z]”,”this phrase” )).

‘This phrase’ returns FALSE for – if (ereg(“str”,”This phrase” )).
‘this phrase’ returns FALSE for – if (ereg(“str”,”this phrase” )).
‘this string’ returns TRUE for – if (ereg(“str”,”this string” )).

Testing email addresses

myemail@myco.com returns TRUE from – if (ereg(“^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$”,$string[$count]))
wrong-email@myco.c returns FALSE from – if (ereg(“^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$”,$string[$count]))
wrong-email@myco.co¬£ returns FALSE from – if (ereg(“^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$”,$string[$count]))

Splitting strings

ereg(“^([^@]+)@([a-z0-9\-]+\.)+([a-z]{2,4})$”,$string,$out)

Note the parentheses.

Address: – myemail@myco.com
Mailbox: – myemail
Domain name: – myco.
Domain type: – com
BUT
Address: – myemail@myco.co.uk
Mailbox: – myemail
Domain name: – co.
Domain type: – uk
AND NOW
Address: – myemail@myco.co.uk
Mailbox: – myemail
Domain name: – myco.co.
Domain type: – uk
Using:-
ereg(“^([^@]+)@([a-z0-9\-]*\.*[a-z0-9\-]+\.)+([a-z]{2,4})$”,$string,$out)

Lesson 9 – Time & Date

Unix epoch time = 1294873385

To increment by Add
1 second 1
1 minute 60
1 hour 3,600
1 day 86,400

Unix epoch time = 1294873385
Formatted date/ time = 12 January 2011 23:03:05
date(“j F Y H:i:s”)

Setting timestamps

$christmas = mktime(10,30,00,12,25,2006); # Note order as h,m,s,mth,day,year

Christmas morning is 25 December 2006 @ 10:30:00
Christmas morning is date(“j F Y @ H:i:s”,$christmas)

$christmas = strtotime(“25 Dec 2006”);

Christmas morning is 25 December 2006 @ 00:00:00

For acceptable formats see http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html

Getting information about a timestamp

$christmas = mktime(10,30,00,12,25,2006); # Note order as h,m,s,mth,day,year

Christmas morning is 25 December 2006 @ 10:30:00
List the components using list($key,$value) = each($TimeInfo)
seconds = 0
minutes = 30
hours = 10
mday = 25
wday = 1
mon = 12
year = 2006
yday = 358
weekday = Monday
month = December
0 = 1167042600

Lesson 10 – Classes

A CLASS is a grouping structure that contains FUNCTIONS (class METHODS) and VARIABLES (class ATTRIBUTES). It has private methods that are hiddden and public methods that are its means of communication. Classes are called in INSTANCES and you can have several instances of the same class used in yoru PHP program called by different names.

The class is normally contained in an INCLUDE file so that it can be reused in other programs. Here’s a simple example.

Code Comment
# First the Class definition
class myClass { # The name of this Class
var $myValue = “Laser”; # A variable with its default value
function myMethod() { # A function definition (the parentheses can hold passed parameters)
echo “myValue is ” . $this->myValue . “< br> “; # The Action carried out.
# $this means in this Class (note the missing $)
}
}
echo “Result:-< br>”; # Mark the output
$myObject = new myClass; # Start a new instance of the Class
$myObject->myMethod(); # Call my method, prints default value
$myObject->myValue = “Topper”; # Change myvalue
$myObject->myMethod(); # Call my method again, prints changed value

Result:-
myValue is Laser
myValue is Topper

Now here’s a real one for validating email addresses


tony.lord@peoplefirst.co.uk is a valid email address



tony.lord@lastmewnzzzersa.co.uk email address could not be validated


Lesson 11 – HTML Forms

Lesson 12 – Dynamic HTML in Forms