Understanding PHP

Before reading this article (er, tutorial?), please be aware these are my views. These views are not right and not wrong. This helps myself, and because I want to help others, I wrote this article for that purpose. This only works for me, so I do not guarantee this’ll work for you. Please note: In order to somewhat understand this tutorial, I suggest you know at least a small amount of PHP.

The Understanding Process

Despite popular belief, PHP is actually server-side scripting language, such as ASP and SQL. Is is not “the new HTML!!!!” It is a server script – whilst HTML is used for designing webpages. Today, PHP is used for making webpages more easily manageable – a security (and advantage) that HTML sometimes cannot provide. Sometimes, in order to achieve this you have to have the basic knowledge of PHP; introducing Understanding PHP

For myself, when I was a newbie, my biggest problem was not learning PHP, but understanding what it was saying. Because I’m currently learning Spanish, when I’m teaching myself words in my head, I’m translating them to English. With PHP, I kind of use that method – if I’m writing out certain things, I can almost speak it.

Using the if() Method

For using the if() method, let’s say I want to list a statement on my “about me” page (I love green apples but hate red and yellow apples!) under a variable:


<?php
$apples = 'I love green apples but hate red and yellow apples!';
?>

I like to think a variable is like a container of sorts to hold information upon information – very useful. After this, we’ll issue the if() method. (Side Note: Make sure when issuing if() statements, that your variables are before the if() statement is issued or it won’t work!)


<?php
$apples = 'I love green apples but hate red and yellow apples!';
if(isset($apples)) {
 echo $apples;
}
?>

‘What in the hell does that mean?’ you say. I like to point out the main feature: $apples. We are making sure that variable is echoed so it will be displayed on your page. So the if() would look like in “normal” language:


PHP: if(isset($apples)) { echo $apples; }
Normal: If the variable $applies is set, we will echo it.

What if I erase it or forget to put it up?, you ask. Of course, there’s always a plan! After the if() statement, we’re going to write out an else statement to back up our if() statement:


<?php
$apples = 'I love green apples but hate red and yellow apples!';
if(isset($apples)) {
 echo $apples;
} else {
 echo 'I love green apples but hate red and yellow apples!';
}
?>

Basically, we’re writing out the variable in case this does happen. Normal language?


P: if(isset($apples)) { echo $apples; } else { echo 'I love green apples but hate red and yellow apples!'; }
N: If the variable $applies is set, echo it; if the variable isn't set, we're going to echo the statement anyway.

The elseif() Secondary Method

Besides else { }, we are able to issue a elseif () { } in case we want to get particular with our “backup” plans. Let’s say that we forget to set the $apples variable, or it ends up being empty. I’m going to specifiy this with our elseif ():


<?php
$apples = 'I love green apples but hate red and yellow apples!';
if(isset($apples)) {
 echo $apples;
} elseif (!isset($apples) || empty($apples)) {
 echo 'I love green apples but hate red and yellow apples!';
}
?>

What is this saying?


P: if(isset($apples)) { echo $apples; } elseif (!isset($apples) || empty($apples)) { echo 'I love green apples but hate red and yellow apples!'; }
N: If the variable is set, echo it; if the variable is not set or is empty, we will re-declare the variable statement whilst echoing it.

Using the foreach() Method

The foreach() method is a little bit more trickier than if(); we are dealing with arrays, which are a little more complex than standard variables. We will start off by going back to apples. Let’s say I want to list my favorite kinds of apples (green, red and yellow) in bold; unfortunately, I don’t want to have to list all of them. With a couple of lines of code, I don’t have to.

First, we’re going to list the apples in a variable inside an array:


<?php
$apples = array('green', 'red', 'yellow');
?>

As said before, I’d love to think variables are containers; this is just another container, holding several containers in one big container. Next, we’re going to issue our foreach() which will provide use with our bold words.


<?php
$apples = array('green', 'red', 'yellow');
foreach($apples as $a) {
 echo '&lt;strong&gt;' . $a . '&lt;/strong&gt;';
}
?>

What does our code say?


P: <?php $apples = array('green', 'red', 'yellow'); foreach($apples as $a) { echo '&lt;strong&gt;' . $a . '&lt;/strong&gt;'; } ?>
N: Apples are in an array; for each item in the array, apply the <strong&gt; tag; echo it.

Of course, there is a backup plan, which involves our previous method: the if() method. Below are three backup plans:
$apples for not being an array, for not existing and for not being set.


<?php
if(!is_array($apples) || empty($apples) || !isset($apples)) {
 $apples = array('green', 'red', 'yellow');
}
[continue code]
?>

English(ly) speaking:


P: <?php if(!is_array($apples) || empty($apples) || !isset($apples)) { $apples = array('green', 'red', 'yellow'); } ?>
C: If $apples is not an array, is empty, or is not set, set the $apples array.

In Conclusion

Regrettably, that is all I have for now. If I do remember other ways to “understand” PHP, I will be sure to revisit this page and update. I hope I at least in some way helped; if I have not, all I can advise is to go to PHP.net » – it is a great help, and will help you understand and work with PHP better. :D

Posted by Austin

Posted on March 16th, 2023

Posted under

  • Articles
  • PHP: Tips and Tricks
  • Understanding PHP
  • Search

    Back to Top ↑