Today I Learned : PHP
- 3min
I was a tad curious and I took a quick glance at PHP. Not quite knowledge to make an app but fun to discover this language.
First, you need to include PHP in your html code:
<? php
// include php code there
?>
Never forget to put semicolons (;) as we must do in JS and CSS.
Basics
Variables
<? php
$str = "I'm learning PHP!";
$nbr = 9;
echo $str; // Prompt I'm learning php
print $nbr; // Prompt 9
?>
Compare values
< > == <= >= !=
Conditionals
If case
if{}elseif{}else{}
Switch case
<? php
switch (2) {
case 0:
echo 'The value is 0';
break;
case 1:
echo 'The value is 1';
break;
case 2:
echo 'The value is 2';
break;
default:
echo "The value isn't 0, 1 or 2";
} ?>
Another way to do the switch
switch($i): case 2: ... endswitch;
Arrays
Create an array
$array = array("Egg", "Tomato", "Beans");
Modify
$array = array("Egg", "Tomato", "Beans");
$array[0]="Omlet"; // Egg remplaced by Omlet
Push
$myTable=array("Sausage"); // [Sausage]
array_push($myTable, "Bacon","Egg"); // [Sausage, Bacon, Egg]
Delete
unset($array[0]); // delete first element
unset($array); // delete the array
Foreach
foreach($languages as $lang){
print "<p>$lang</p>";
}
Sorting the array
$myTable=array();
array_push($myTable, "Bacon","Sausage");
print count($myTable);
sort($myTable); // sortin array
print join(",",$myTable); // returns elements from array into a string with a separator
resort($myTable); // reverse sorting
print join(",",$myTable);
Compute difference between arrays
$A = array("a" => "green", "red", "blue", "red");
$B = array("b" => "green", "yellow", "red");
$result = array_diff($A, $B); // ["blue"]
array_diff($A,$B)
returns all elements from $A which are not elements of $B.
String object manipulation
$str="myString"; //create a string
$len=strlen($str); //length of your string
$str=strtoupper($str)//put the string in upper case
$str=strtolower($str) //in lower case
/*substr(string,position first letter,number of letters to display)*/
$str=substr($str,0,3) // "myS"
$pos=strpos($str, 'y'); // get the position of 'y' in $str
if(strpos("alex", "v")===false){
echo "The letter v doesn't appear in the string";
}
$name="Alex";
$concat="Greetings,".$name."!"; //Concatenation
echo $concat;
Form to send e-mail
<?php
// Configuration
$config = array(
'success' => 'Your message is succesfully send',
'error_name' => 'You must enter a name',
'error_firstname' => 'You must enter a firstname',
'error_mail' => 'E-mail not valid',
'error_message' => 'You must write a message',
);
$_POST['name'] = htmlspecialchars(addslashes($_POST['name']));
$_POST['firstname'] = htmlspecialchars(addslashes($_POST['firstname']));
$_POST['email'] = htmlspecialchars(addslashes($_POST['email']));
$_POST['message'] = htmlspecialchars(addslashes($_POST['message']));
$d['errors'] = array(); // Errors stack
// Check name
if(empty($_POST['name']))
{
$d['errors']['name'] = $config['error_name'];
}
// Check firstname
if(empty($_POST['firstname']))
{
$d['errors']['firstname'] = $config['error_firstname'];
}
// Check e-mail
if(empty($_POST['email']) || !preg_match('#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#', $_POST['email']))
{
$d['errors']['email'] = $config['error_mail'];
}
// Check message
if(empty($_POST['message']))
{
$d['errors']['message'] = $config['error_message'];
}
// Send e-mail
if(empty($d['errors'])){
$d['errors'] = false;
$receiver = 'contact@domain.com';
$subject = 'Contact';
$name = strtoupper($_POST['name']);
$firstname = strtolower($_POST['firstname']);
$email = $_POST['email'];
$message = "<html><head><style>h1, h2, h3 { color: #850BC2; } h3 { text-decoration: underline; line-height: 25px; } #corps_msg { width: 600px; margin: 0 auto; border: 2px solid #850BC2; -moz-border-radius: 20px; -webkit-border-radius: 20px; -khtml-border-radius: 20px; -ms-border-radius: 20px; -o-border-radius: 20px; border-radius: 20px; padding: 10px 30px; } li, p { font-size: 16px; line-height: 30px; text-align: justify; }</style></head><body><div id='corps_msg'><h1>Hi!</h1><h2>New message!</h2><h3>From :</h3><ul><li><strong>Name:</strong> " . $name . "</li><li><strong>Firstname:</strong> " . $firstname . "</li><li><strong>E-mail:</strong> " . $email . "</li></ul><h3>Message recieved:</h3><p>" . $_POST['message'] . "</p></div></body></html>";
$headers = "From: " . $name . " " . $firstname . " <" . $email . ">\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
$headers .= "Content-Transfer-Encoding: 8bit";
mail($receiver, $subject, $message, $headers);
$d['errors']['sendOK'] = $config['success'];
}
echo json_encode($d);
?>