Strings and variables
Print as html |
echo "This is my string"; |
This is my string |
String |
"Hello World!"; |
Hello World! |
Escape sequence |
"Hello \"World\"!"; |
Hello "World"! |
Concatenation (Literal) |
echo "one" . "two"; |
onetwo |
Variable creation and assignment |
$var = "Hello"; |
Variable contatenation |
echo $var . " you"; |
Hello you |
Variable parsing |
echo "$var you"; |
Hello you |
Expanding string |
echo "${var}oo! you!; |
Hellooo! you! |
Concatenating assigmente |
$var .= " world"; |
Hello world |
Assign by reference |
$var1 = $var2; |
Types and operators
String |
$string = "Hello"; |
Int |
$int = 4; |
Float |
$float = 4.2; |
Exponentiation |
4**2 --> (4^2) = 64 |
Modulo |
7 % 3 --> 1 |
Mathematical assignment operator |
$int += 3; |
Increment and decrement |
$int ++; |
Global variable |
global %var; //Inside a function to access a value declared outside the function |
- Result of adding up two floats 9.9 and 1.0 will return an integer, since the result is 10 and evaluates to a whole number. Applies to every calculation. The reverse is also true.
- Operations order: () >> * >> and / >> + and -
Functions
function greeting($value = "Sam") // Default value = Sam
{
return "Hello $value!";
}
$return_value = greeting("Tom");
echo $return_value; //Prints: Hello Tom!
echo greeting(); //Prints: Hello Sam!
function addXPermanently (&$param) // Passed by reference
{
$param = $param . "X";
echo $param;
};
$word = "Hello";
addXPermanently($word); // Prints: HelloX
echo $word; // Prints: HelloX
|
Built-in Functions
gettype(4); // Prints: integer |
Get the type of the parameter given as a string |
var_dump(4); // Prints: int(1000000) |
Prints details about the argument |
strive("Hello"); // Prints: olleH |
Prints the string in reversed order |
strtolower("Hello"); // Prints: hello |
Lower case the string |
str_repeat("Hello", 2); // Prints: HelloHello |
Repeats the string the number of times specified |
substr_count($string, $substring); |
Number of instances of a substring within a string |
abs(-2); // Returns 2 |
Returns the absolute value |
round(3.8); // Returns 4 |
Rounds a number |
rand(); |
Returns a random number between 0 and the largest allowed |
rand(3, 10); |
Returns a number between 3 and 10 inclusive |
getrandmax() |
Returns the largest random number value possible |
|
|
Data Structures
Array |
$my_array = array(0, 1, "A"); |
Array |
$my_array = [0, 1, "A"]; |
Print array |
print_r($my_array); |
Returns array separated by value |
implode(", ", $my_array); |
Access array index |
$y_array[1]; |
Adds the element at the end of the array |
$my_array = "new element"; |
Replace the element |
$my_array[0] = "new first element"; |
Pops the last element and returns it |
array_pop($my_array); |
Push the elements to the end of the array and return its size |
array_push("A", "B"); |
count($array); // Returns the number of elements in the array
- Different types are allowed in an array
Map
Associative array
$my_array = ["panda" => "very cute", "lizard" => "cute", "cockroach" => "not very cute"];
Array function
$about_me = array(
"fullname" => "Aisle Nevertell",
"social" => 123456789
);
echo implode($my_array); // Prints only the values
print_r($my_array); // Prints keys and values
$my_array = ["new"] = "new item"; // Adds new element
$favorites = ["favorite_food"=>"pizza", "favorite_place"=>"my dreams", "FAVORITE_CASE"=>"CAPS", "favorite_person"=>"myself"];
echo $favorites["favorite" . "_" . "food"];
// Prints: pizza
$key = "favorite_place";
echo $favorites[$key];
// Prints: my dreams
echo $favorites[strtoupper("favorite_case")];
// Prints: CAPS
unset("my_array["new"]); // Delete the "new" element if exists
|
key => value
Works as a dictionary. Arrays are maps where the key is the index.
Key can be number or characters.
Libraries
<?php include 'footer.html';?> |
Includes the HTML file where specified |
<?php require 'somefile.php'; ?> |
Requires the specified file |
|
|
PHP & HTML
<?php
$lucky_number = 5 * 2 - 1;
echo "<h1>Your lucky number is ${lucky_number}</h1>";
?>
<?php
function makeHeaderGreeting ($name){
return "<h1>Hello, ${name}!</h1>";
}
echo makeHeaderGreeting("World");
?>
$about_me = [
"name" => "Aisle Nevertell",
"birth_year" => 1902,
"favorite_food" => "pizza"
];
function calculateAge ($person_arr){
$current_year = date("Y");
$age = $current_year - $person_arr["birth_year"];
return $age;
}
?>
<h1>Welcome!</h1>
<h2>About me:</h2>
<?php
echo "<h3>Hello! I'm {$about_me["name"]}!</h3>";
echo "<p> I'm " . calculateAge($about_me). " years old! That's pretty cool, right?</p>";
echo "<div>What more is there to say? I love {$about_me["favorite_food"]}, and that's pretty much it!</div>";
?>
|
Data access in Forms
$_GET |
Non sensible information |
$_POST |
Used to send sensitive information |
$_GET['user'] |
Retrieves the value sent through the user get form |
$_isset($_POST['send']) |
Check if send has a value |
When a form is submitted values are received contained in an associative array.
|