Array ( [0] => 3 [1] => 4 [2] => five [3] => six )
It will get the values from position 2 onwards
$myArr = array("one","two", 3, 4, "five","six");
$slicedArr = array_slice($myArr, -2, 1); // -2 means, 2nd from the last; 1 means, how many values to show including the one in current position
print_r($slicedArr);
โ prints out โ
Array ( [0] => five )
Shuffle Array
$myArr = array("one","two", 3, 4, "five","six");
shuffle($myArr); // randomise the arrangement of values
print_r($myArr);
โ prints out random position of โ
Array ( [0] => six [1] => two [2] => five [3] => 3 [4] => one [5] => 4 )
Variable Check
if(empty($a)){
echo 'empty';
}else{
echo $a;
};
This helps as it wonโt throw error if $a is not declared.
$a = "hello";
echo strlen($newStr); // get the length of string
โ prints out โ
5
Get letter of string in specified position
$a = "hello";
echo $a[1]; // get 2nd letter of string
โ prints out โ
e
Add string to existing string in a specified position
$a = "hello";
$a[20] = "world"; // add 'world' to position 20 of $a
echo $a;
โ prints out โ
hello w
Even though we added world to $a[20] but only w was added in.
From here if we echo strlen($a);, we will get 21
And if we view source, we will see
hello w
On frontend, we see hello w even though when we view source, there are many white space. That's because html converts multiple white spaces into just 1 white space, so on frontend we will only see hello w.
Other useful string functions:
lcfirst โ Makes 1st character of string to lowercase
strtolower โ Make a string lowercase
strtoupper โ Make a string uppercase
str_split โ Convert a string to an array
htmlentities โ Convert all applicable characters to HTML entities
html_entity_decode โ Convert HTML entities back to browser readable format
PHP Arrays
Counting Arrays
โ prints out โ
Unset Arrays
When we unset a value from array, it will be removed. Thatโs why after unset, there are only 5 values left.
โ prints out โ
Reversing Arrays
โ prints out โ
Array Slice
Another commonly used function is
array_slice
. https://www.php.net/manual/en/function.array-slice.phpโ prints out โ
It will get the values from position 2 onwards
โ prints out โ
Shuffle Array
โ prints out random position of โ
Variable Check
This helps as it wonโt throw error if $a is not declared.
String Functions
Shuffle String
โ prints out โ
Get length of string
โ prints out โ
Get letter of string in specified position
โ prints out โ
Add string to existing string in a specified position
โ prints out โ
Even though we added
world
to$a[20]
but onlyw
was added in.From here if we
echo strlen($a);
, we will get21
And if we view source, we will see
On frontend, we see
hello w
even though when we view source, there are many white space. That's because html converts multiple white spaces into just 1 white space, so on frontend we will only seehello w
.Other useful string functions:
lcfirst
โ Makes 1st character of string to lowercasestrtolower
โ Make a string lowercasestrtoupper
โ Make a string uppercasestr_split
โ Convert a string to an arrayhtmlentities
โ Convert all applicable characters to HTML entitieshtml_entity_decode
โ Convert HTML entities back to browser readable formatExample for
str_split
:โ prints out โ
If we insert a 2nd variable in
str_split
, it will be like this:โ prints out โ
Notice that
[0]
and[2]
have 2 characters each, but[1]
has 3 characters. This is because the string is split based on bytes rather than charactersExample for
htmlentities
:โ prints out โ
If we want to convert it back to readable HTML format, we need to use
html_entity_decode
:โ prints out โ
More string functions at https://www.php.net/manual/en/ref.strings.php