Skip to content

Phils Blog and Stuff

TELECOMS. VIRTUALIZATION. IT. CODING. and more…

Menu
  • Home
  • Resources
    • Subnet Mask Cheat Sheet
    • Easy Dmarc+Email
    • MX Tool Box
    • LOAD BALANCING & SRE
  • SHOP
  • SOLUTIONS
  • SERVICES
  • Contact Me
Menu

Some Simple PHP code..

Posted on 14/11/201208/03/2023 by Phil

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.

POSTS

  • Home Assistant. Add water meter with PHP+MQTT+YML.
  • Automating your home with Home Assistant. Initial install and config (Hyper-V).
  • The importance of setting up email security correctly.
  • Automate Debian installations with Seed, DHCP and Nginx.
  • A simple link shortener.. Fun little project…

WORD CLOUD

3CX 3CX Phone System Apache Asterisk Cacti CentOS CRM DHCP DNS Email Fail over Failover File System Firewall FreeBSD FreeNAS FXO IAX install IP IP PBX Linux M0n0wall Nottingham Open Source PBX PFSense PHP Router Server SIP snom SSH TrixBox ubuntu VLAN Voice VoIP VPN vyatta WAN Website WiFi yealink ZFS

© 2025 Phils Blog and Stuff | Powered by Superbs Personal Blog theme