cyber-s / blog

tipsใ‚„่ณชๅ•ใชใฉใ‚’ใ“ใ“ใซknowledgeใจใ—ใฆๆฎ‹ใ—ใพใ™ใ€‚
0 stars 0 forks source link

๐Ÿค“ e-Learning Journey - PHP #694

Open huirongcs opened 4 years ago

huirongcs commented 4 years ago

PHP Arrays

Counting Arrays

$myArr = array("one","two", 3, 4, "five","six");
echo count($myArr); // count number of values in array

โ†‘ prints out โ†“

6

Unset Arrays

When we unset a value from array, it will be removed. Thatโ€™s why after unset, there are only 5 values left.

$myArr = array("one","two", 3, 4, "five","six");
unset($myArr[2]); // remove 3rd value from array
echo count($myArr);

โ†‘ prints out โ†“

5

Reversing Arrays

$myArr = array("one","two", 3, 4, "five","six");
$myArr = array_reverse($myArr); // reverse array arrangement
print_r($myArr);

โ†‘ prints out โ†“

Array ( [0] => six [1] => five [2] => 4 [3] => two [4] => one )

Array Slice

Another commonly used function is array_slice. https://www.php.net/manual/en/function.array-slice.php

$myArr = array("one","two", 3, 4, "five","six");
$slicedArr = array_slice($myArr, 2);
print_r($slicedArr);

โ†‘ prints out โ†“

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.


String Functions

Shuffle String

$a = "hello";
$newStr = str_shuffle($a); // shuffles the string
echo "$newStr <br>";

โ†‘ prints out โ†“

lheol

Get length of string

$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

Example for str_split:

$str = "Hi there";
$arr1 = str_split($str);
print_r($arr1);

โ†‘ prints out โ†“

Array ( [0] => H [1] => i [2] => [3] => t [4] => h [5] => e [6] => r [7] => e )

If we insert a 2nd variable in str_split, it will be like this:

$arr2 = str_split($str, 3);
print_r($arr2);

โ†‘ prints out โ†“

Array ( [0] => Hi [1] => the [2] => re )

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 characters

Example for htmlentities:

$a = "<p>Hi there</p>";
echo $a."<br>";
echo htmlentities($a);

โ†‘ prints out โ†“

Hi there

<p>Hi there</p>

If we want to convert it back to readable HTML format, we need to use html_entity_decode:

$b = htmlentities($a);
echo html_entity_decode($b);

โ†‘ prints out โ†“

Hi there

More string functions at https://www.php.net/manual/en/ref.strings.php

justcallmehide commented 4 years ago

๐Ÿค“