Disclaimer – there are likely to be mistakes in this, so please point them out! Here are my notes from my PHP for beginners course, which was excellent, and was run by Highlander. I know it’s better to use the PHP manual but it helps me to write these out so feel free to ignore these!
- boolean = true or false
- int = integer
- float = fraction
- string = text, in quotes
- resource = external (not native to PHP) resource
- $myArray = array (“keyname1″=>”value1”, “keyname2″ =>”value2”);
- If no key name is provided, the default is 0, 1, 2… etc.
- [square brackets are used to ACCESS array contents]
- (normal brackets are used to SET UP functions and arrays)
this will print on two
lines, even without a br tag
$x = $x + 1;
$n = 3;
echo $n * $n- -;
// (ignore spaces between minus symbols) equals
echo $n * ($n- -);
// two minus symbols after $n means run the line then apply (-1) to $n
$n = 3;
echo $n * – -$n;
// two minus symbols before $n means apply (-1) to $n THEN run whole line.
$a = 10;
$b = $a; // SET b to value of $a (10)
$b = 20;
echo $a; //outputs 10
$a = 10;
$b = &$a; // the ‘&’ LINKS $a and $b
$b = 20;
echo $a; // outputs 20
== means are they the same VALUE?
=== means are they the same VALUE AND TYPE (e.g. integer or string)?
$array = array (); // creates an empty array
$array  = array(‘a’,’b’); // puts array(‘a’,’b’) INTO slot 0 of $array
$a = array(1,2,3);
$b = array(‘a’=>1,’b’=>2,’c’=>3);
$c = array(1,2,3);
$a+$b will produce array(1,2,3,1,2,3)
$a+$c will produce array(1,2,3) //as values and keys are the same
- isset checks if a key has ANY value
- array_key_exists checks if a key exists
- in_array checks is a CERTAIN VALUE exists
- array_flip swaps keys for their values
- array_reverse reverses ORDER of values – keys remain in place
$a = array(‘a’=NULL);
echo isset($a[‘a’]) //will FAIL because NULL means no value
echo array_key_exists(‘a’,$a) //will PASS (because ‘a’ exists)
echo in_array(2,$a) //will FAIL because there is no ‘2’ value in $a
//will destroy keys and put values in alphabetical order, assigned to default numerical keys, i.e. 0,1,2…
//keeps keys and arranges in order of values alphabetically
Ascending order (A->Z) is default. Descending order uses rsort() and arsort().
sort would do 1,20,11,2,3 etc.
natsort realises that 10 is bigger than 9, so does 1,2,3…8,9,10,11….
ksort //puts in alphabetical order by KEY
krsort //orders alphabetically in descending order by key
shuffle //puts values in random order (keys destroyed and reassigned as 0,1,2…)
array_keys() //puts KEYS into their own array, in the order they are already in (doesn’t sort alphabetically).
array_push($stack,’foo’,’bar’); //adds foo, then bar, to array $stack.
array_diff($a,$b) //produces any VALUES in $a which are NOT in $b. Keys are not compared, but are KEPT.
array_intersect($a,$b) //returns ‘duplicate’ values (that are found in $a AND $b), irrelevant of key, but key from $a is kept. If two ‘x’ values are found in $a and one is found in $b, both values from $a are returned.
If using $something in a string, use double quotes. Only use single quotes for plain text to be output literally (not processed).
PHP_EOL //end of line
error_reporting(0); //returns all errors. Other numbers limit error messages to certain ones.
In a form, if you set the name of multiple items to something, this tells PHP they are all in the same array.
setcookie(“filename_of_cookie”,”content_of_cookie”); //creates cookie
setcookie(“filename_of_cookie”,false); //deletes cookie
rand(0,100); //gives a random number between 0 and 100
mysql_fetch_row //automatically repeats through all rows
Applications used: database browser – Navicat. mySQL Administrator. XAMPP Control Panel.
On navicat, where it shows a lock on a folder, it’s THAT ONE that has some privileges ALLOWED. No lock means NO privileges at all for that user have been given.
$_GET gets info from the URL.
echo “<form name=\”search Form\”action=\”search.php\”method=\”get\”>”;
// The ‘get’ here, puts whatever is input, into the URL.
echo “<select name=\”mysearch\”>”;
//mysearch is put into ULR e.g. http://www.something.com?mysearch=input