Below i am going to list some simple PHP code that makes up most of a PHP coded page.
If..Elseif…Else… statments
$something = "Cat";
if ($something=="Cat") {
print "Cat\n";
} elseif ($something=="Dog") {
print "Dog\n";
} else {
print "Dont know\n";
Explode…foreach
$ex = "cat:dog:mouse:goose";
$explode = explode(":", $ex);
foreach ($explode as $exploded) {
print "Explode=$exploded\n";
//result
//
// Explode=cat
// Explode=dog
// Explode=mouse
// Explode=goose
//
foreach
$stuff = array("cat", "dog", "mouse", "goose");
foreach ($stuff as $something1) {
print "something=$something1\n";
}
//result
//
// something=cat
// something=dog
// something=mouse
// something=goose
//
Switch case
$something = "something2";
switch ($something)
{
case "something1":
print "something 1";
break;
case "something2":
print "something 2";
break;
case "something2":
print "something 2";
break;
default:
Print "Can not find something";
}
//result
//
// something2
//
Simple File writing
$myFile = "somefile.txt"; $fh = fopen($myFile, 'w') $cont = "TEXT\n"; $cont .= "IN\n"; $cont .= "THIS"\n"; $cont .= "FILE"\n"; fwrite($fh, $cont); fclose($fh);
Difinitions
At the start of the php file, but not limited to the start place-
define("DEFINE", "something");
Later in the PHP code/file, do something with the DEFINE
print ".DEFINE."; // Result // something
functions
To Create a function do the below
function something($something) {
print "something = $something";
}
To call the function and to get it to do something
something("name");
// result
// something = name
Loading a simple XML file in php
Create a XML Source called xmltest.xml and place the code below in there
Create a php file with the below code in there
$config_file = "xmltest.xml";
if (file_exists($config_file)) {
$xml = simplexml_load_file($config_file);
$ID = $xml->user_details_001->ID;
$fname = $xml->user_details_001->first_name;
$lname = $xml->user_details_001->last_name;
$house_number = $xml->user_details_001->house_number;
$street = $xml->user_details_001->street;
$town = $xml->user_details_001->town;
$city = $xml->user_details_001->city;
$postcode = $xml->user_details_001->post_code;
print "$ID\n";
print "$fname\n";
print "$lname\n";
print "$house_number\n";
print "$street\n";
print "$town\n";
print "$city\n";
print "$postcode\n";
} else {
exit('Failed to open xmltest.xml.');
}
this should be able to parse a simple XML file, from here you can load the XML elements in to other things.
