/The-PHP-Practitioner

Repo for the Laracasts course.

Primary LanguagePHP

The-PHP-Practitioner

Repo for the Laracasts course.

Single vs. double quotes

From PHP double quotes vs single quotes by Virendra Chandak:

Single quoted strings are the easiest way to specify string. This method in used when we want to the string to be exactly as it is written. When string is specified in single quotes PHP will not evaluate it or interpret escape characters except single quote with backslash (‘) and backslash() which has to be escaped.

echo 'This is \'test\' string';
//Output: This is 'test' string

In double quoted strings other escape sequences are interpreted as well any variable will be replaced by their value.

$count = 1;
echo "The count is $count";

Separate PHP Logic From Presentation

Instead of nesting PHP in HTML, but: separation of concerns!

We create a file that will be in charge of displaying the data generated by the PHP. We can build a view and call it: index.view.php.

We prepare the data and display it (we load rthe view). For this, we need to call the view file in the index.php file:

`require 'index.view.php';

Understanding Arrays

The syntax for arrays (the equivalent of a Python list) is just:

$animals = [
    'dog',
    'cat',
    'bird'
];

Looping through an array in PHP is done through the foreach keyword:

foreach ($animals as $animal) {
    echo $animal . ', ';
}

Note that in PHP, the for loop syntax is: For each item in the array, this is what we do with every single item. It's kind of the opposite of Python where we say instead: For each item in the array, we do this....

What we call the single item we'll reference in the loop doesn't matter.

How does this work in a view?

We could bring the markup in PHP like:

<ul>
    <?php
    
    foreach ($animal as $animal) {

        echo "<li>$animal</li>";
    }
    
    ?>
</ul>

But that's a bit sloppy. There's a better shorthand for HTML:

<ul>
    <?php foreach ($animal as $animal) : ?>

    <li><?= echo $animal; ?></li>

    <?php endforeach; ?>

</ul>

The differences are: No curly braces (we use : instead) and we close the for loop with endforeach. It's much better if we have a lot of markup: We don't have to enclose the HTML in double quotes, which can get kind of messy fast.

Associative Arrays

Basically a dictonnary in Python: a set of key/values pairs. The assignement is a bit funny looking:

$person = [
    'age' => 31,
    'hair' => 'brown',
    'job' => 'web developer'    
];

When calling an associative array in a foreach loop, only the values will be returned:


<ul>

    <?php foreach ($person as $feature) : ?>

    <li><?= $feature; ?></li>

    <?php endforeach; ?>

</ul>

If we want to return both the keys and the values, we'll have to do it this way:

<ul>

    <?php foreach ($person as $feature => $value) : ?>

    <li><strong><?= $feature; ?>: </strong><?= $value; ?></li>

    <?php endforeach; ?>

</ul>

Notice how the $feature => $value syntax mirrors the way values are assigned to keys.