Did you know FaceySpacey is the creator of Redux-First Router and Universal?

The Startup Incubator.
Your one stop social media shop
Resource Categories:

Technical
Non-Technical
Refer A Friend
Popular Articles
DEVELOPMENT TOOLS 1 - VERSION CONTROL (Mercurial)
CONTRACTING 2 - TOP 10 EASY SITE CREATOR TOOLS
Subscribe
-
Technical
-
PHP
-
OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS) OOP PHP 3 - INHERITANCE IN OOP PHP 1 - Introduction To Programming PHP 2 - DYNAMIC CODE PATHS & VARIABLES PHP 3 - FUNCTIONS PHP 4 - SCOPE PHP 5 - HOW TO LEARN PROGRAMMING PHP 6 - ARRAYS PHP 7 - LOOPS PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
- PHP 2 - DYNAMIC CODE PATHS & VARIABLES
PHP 2 - DYNAMIC CODE PATHS & VARIABLES
This tutorial on PHP should be treated as a tutorial about learning the basics of all programming. It’s goal is to be an eye-opener into what programming is about.
The reason we think PHP is the language to start learning programming through for the web developers is two-fold:
1) it’s syntatically similar to Javascript, which all web developers will have to learn, since it’s the language of the browser (note: HTML works in conjunction with javascript in the browser, where HTML deals with simple display & presentation, while javascript does the heavy lifting necessary for animations, and ajax calls to your server that don’t require a page refresh). PHP is also syntactically similar to C/C++ which is basically the grandfather of all modern procedural & object oriented programming.
2) it’s the easiest language to setup on a server and get productive with, as it’s the most prevalent web development server side language. That also means it has the most resources to learn it and all its tools.
So back to the tutorial. I like to start be introducing if/else statements since it does a very good job at highlighting how programming is really all about basic logic you already know. Code example time:
$variable = 2; if ($variable == 2) { echo ‘the variable equals 2’; } else { echo ‘this code path will not get executed currently’; }
So that code if executed will always echo out “the variable equals 2” to the browser. That’s fine. Quick note: a variable in PHP is any word that starts with a $ symbol. Simply a variable is a placeholder that at different times can hold different values. If you don’t fully understand variables yet, just look out for any $words like that--you’ll understand soon. So lets break down a few important things going on here:
1) THE FOLLOWING IS THE ASSIGNMENT OF THE VALUE 2 TO A VARIABLE CALLED $variable:
$variable = 2;
2) THE FOLLOWING TESTS WHETHER $variable EQUALS 2 (THE DOUBLE EQUAL SYMBOLS HERE IS THE DIFFERENCE BETWEEN THIS COMPARISON AND THE ASSIGNMENT IN #1 ABOVE):
if ($variable == 2)
3) THE FOLLOWING CODE BETWEEN THE BRACKETS DELINEATES 2 SEPARATE CODE PATHS. THE CODE WITHIN THE BRACKETS IS OBVIOUSLY WHAT GETS EXECUTED DEPENDING ON WHICH PATH IS TAKEN. WHAT PATH IS TAKEN IN THIS EXAMPLE IS THE FIRST PATH SINCE THE COMPARISON RESOLVES TO TRUE. OTHERWISE THE “ELSE” PATH WOULD BE TAKEN:
} else { echo ‘this code path will not get executed currently’; }
So let’s make this a bit more “dynamic,” which as you may recall means changing. Let’s try to make it so it’s possible for both of the 2 code paths to be taken:
$dayOfMonth = date('j'); if ($dayOfMonth < 15 ) { echo "it’s the first half of the month"; } else { echo "it’s the second half of the month"; }
So clearly this will execute one code path the first half of the month and the second code the second half of the month. Don’t worry about date(‘j’) just yet. That’s obviously finding the day of the month. It’s called a function. We’ll get to it later in detail. They’re actually quite simple to grasp. Simply you provide it some input in the parentheses and it gives you a response, which in this case is assigned to the variable $dayOfMonth. Basically the date() function is connected to your server’s clock and knows the date and time, and in this case is returning the number for the current day of the month.
So to wrap up your first PHP and programming in general tutorial, i’d like to simply point out the most important thing that you learned: languages like PHP allow you to dynamically determine to different code paths, i.e. different results to display to the end user. That’s the main goal. That’s the main birds-eye-view concept to really get a hold of before moving on.
Related Entries
- OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP
- OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS)
- OOP PHP 3 - INHERITANCE IN OOP
- PHP 1 - Introduction To Programming
- PHP 2 - DYNAMIC CODE PATHS & VARIABLES
- PHP 3 - FUNCTIONS
- PHP 4 - SCOPE
- PHP 5 - HOW TO LEARN PROGRAMMING
- PHP 6 - ARRAYS
- PHP 7 - LOOPS
- PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
Comments

SMM 3 - FORMULA TO FIND INFLUENCERS