Let's start with the fundamentals
Basic Tools/Apps, this will be general more so than a deep dive. PHP is to learn, easy to get a setup running and compatible. Some basic tools, a machine - PC, MAC or Linux. A text editor of some sort, we use Sublime Text or Notepad++. Links will be in the reference section, along with a quick description.
Next we need a server to server up our files, run Apache, MySQL and PHP. We recommend MAMP (we run MacOS), there are WAMP and LAMP as well depending on the OS you are using. MAMP does exactly that, it allows the user to target a folder on your computer, stand up a local server and launch site pages in real time. Voila! We are a go!
Side Note: This is meant to get you started, there are tons of other tools and applications out there for you to sort out. Have at it. Also, anyone can do these few steps without an internet connection per say, localhost is running in the backround. Of course, it's helpful to have an internet connection, but it's not required to run your own local server.
Let's start there for the setup process, links and descriptions can be found on the reference page noted in the main navigation. It is helpful to have some knowledge of coding, whether that may be HTML, CSS, JS, Python etc, this will help move more fluidly through the content. If not, a generic HTML file structure will be helpful (see below), or check the reference section for a broader example.
Sample HTML Code
<?php /* This will be a generic HTML file save as index.php */ ?>
<?php echo "hello world"; //return hello y'all ?>
So, What's Next? What is PHP?
PHP stands for Hypertext Pre-Processor, PHP is a scripting language that is a server side langauge, widely used and compatible with most systems. PHP files are saved with the \.php\ extension and work well with other code. We will start with the classic developers call below, say hi to the PHP world with PHP.
Example 1 - Print Hello World
<?php echo "hello y\'all"; ?>
Output: hello y'all
In this example, we are using multi-line /* */ and single line // comments. Comments are super helpful. We will cover comments a bit further down... Not to get ahead of oneself, but comments are very useful when making notes as comments are not processed by the browser or server in this case.
Also, we use the '\' key to negate the single quote between y and all. Again, further explaination will be required, in short we need a way to show the single quote and without the keystroke, we may execute code, break code or leave elements open. Try it out, it's handy to know global keystrokes for multiple languages.
Various Data Types
What are data types you ask? Well, they are forms of data that the browser or server will interpret as such. In this case, we have a few and they are as follows: Integers, Floating Numbers (Floats), Character Strings (strings) and Boolean.
It might be a mouthful, but let's explain a bit further, we classify data with the following data types... So, we have numbers, numbers with decimals, alphanumeric characters and true/false.
Example 2 - PHP Integer Max
<?php echo PHP_INT_MAX; ?>
Before we close out Data Types, let's incorporate variables which determins what will show or what is visbile to the end user.
Let's jump into variables very quickly... So, we use variables to store data and/or provide stored data. We can use local variables, global variables which will be accissible when using PHP, accross the code base. How about some examples?
<?php $var; //start with $ and a letter ?>
<?php $var_car; //case sensitive ?>
<?php $var_car1; //contain numbers ?>
Example 3 - PHP Variables, starting with an integer
<?php $my_var = 1; echo $my_var; //variable as an integer, print ?>
Example 4 - PHP Floating Numbers
<php $my_var = 3.14; echo $my_var; //Variable as a float, print ?>
Example 5 - PHP Character string
<php $my_var = "Hypertext Pre Processor"; echo $my_var; //Variable as a String, print ?>
Output: Hypertext Pre Processor
Example 6 - PHP Explicit type casting
<php $a = 1;
$b = 1.5;
$c = $a + $b;
$c = $a + (int) + $b;
echo $c;//Variable as an Integer 1 + 1.5 (as an integer is 1) equals 2, print ?>
Example 7 - PHP Dump Func. to determine data type
<php $a = 1;
$b = 1.5;
$c = "I love PHP";
$d = true
//Dump variable values ?>
Output: int(1) float(1.5) string(10) "I love PHP" bool(true)
We will not cover PHP constant, or values that can't be changed, think value for PI or 3.14. This is a constant. Check out the reference for more details. Wrapping up variables and data types, give the examples a try, create other variables to test or check out variables in the reference section.
Moving onto Operators, think Sherlock Holmes, 'Elementary Dear Watso, elementary...' arithmetic. We need to use operators to 'perform' our computations, same for the inclusion of variables, data types etc.
Example 8 - PHP Arithmetic (numerical data types) Operators+ - Addition, add x and y
- - Subtraction, difference between x and y
* - Multiplication, multiplies x and y
/ - Division quotient of x and y
% - Modulus (percentages with remainder)
-n - Negation, negative number
x.y - Concatentation, put x and y together
Example 9 - PHP Assignment Operatorsx = ? - assignment, x value to 5, therefore x = 5
x += ? - addition, x increments the value of x = 3
x -= ? - subtraction, x subtracts the value of x = 1
x *= ? - multiplication, x multiplies the value of x = 0
x /= ? - Division, x quotient the value of x = 2
x %= ? - subtract, x subtracts the value of x= 1
x .= ? - Concatentates, puts values together x = Pretty Cool stuff
Example 10 - PHP Comparison Operatorsx == y - Does x = 1
x === y - Is x identical, x = 0 or False
x != y, x < > y - 'not equal', comparing x and y1 / 1, Therefore it is True or 1
x > y - Greater than1, Therefore it is True or 1
x > y - Less than, Therefore it is False or 0
x > y - Greater than or equal1, Therefore it is True or 1
x > y - Less than or equal, Therefore it is False or 0
Example 11 - PHP Logical Operators:x and y, x && y - And, return true if both x and y are equal (true) 10
x or y, x || y - Or, return true if either x and y is true 11
x!, x not 0 1
As we discussed in the first section, with our first PHP code example, when printing 'Hello World' to the page. We briefly covered comments. A few more points now, single comments can be used 'locally' or inline to descibe code operation. Same for the multi-line comments, if you need more than one line, give a multi-line comment a try. We find ourselves using multi-line comments at a global level, when describing sections, or program level information that another developer might use when reading, validating or breakfix code.
Example 11 - PHP Logical Operators
<php //single line
***** Multi Line comments for more information that may spill over to multiple lines *****
No output here, although the example shows you the dev how to add comments to your own code examples. We use it for notes, troubleshooting or ease of use.
Use comments wisely, there is always a balance between comments and code, know that there is memory being used when using comments. We've covered the barebones per say with variables, operators, datatypes, comments, how to print to the screen and stand up PHP in a dev environment. Let's move onto including 'stuff' along with your PHP code.
PHP Include files
So, let's include another file, in this case the file called 'demo.php'. Two, use the old click/clack keyboard and type in your question over on the offical PHP.net you can always reference the documentation there. Better yet, a validator is a great way to see how code is processed, or not... might be worth your while, check out the PHP Code Check. Fine line between what is symantically correct and correct from a processing perspective.
Example 12- PHP includes/include_once file 'demo.php'
echo "Show me the first includes statement:"; include 'demo.php'; //display content from demo.php
echo "Show me the first includes statement:"; include_once 'demo.php'; //display content from demo.php
Output: Show me the first includes statement: hello world...
Now, try 'required', which also has two flavors so that you as the developer can include 'stuff' with the code being created. So, what's the difference here? We've got includes and required, what's the difference? Includes will kick out an error when it occurs, script will continue although there is an error. On the flip side, required does not post a warning and the script will stop when the error occurs.
Example 14 - PHP required exampleOutput: config.php;
We end with arrays, as the last part of this fundamentals section. Listen there is a lot of information to digest. Don't worry, just keep trying... Fail fast, the worst case is you have to find another example or another. Just keep plugging away at it.
Arrays, this is the 'test' before we move into Logicals. Arrays store data, also use numbers to access that data called keys. The access key is a memory slot. Ideally, when you need to access the stored data, we used these access key to extract the data or assign it to something else.
Example 15 - PHP arrays
$var[n] = value;
In the example above, you'll see a variable name, 'n' which is it's memory slot or access index number/key and a value. Let's take this example and expand it to include a real life example. How about we look at some cars, and use those cars in a statement.
Example 16 - PHP arrays
$car = 'Volvo Wagon';
$car = 'VW Bus w 27 Windows';
$car = 'Audi A6 Wagon';
$car = 'Merc Wagon';
$car = 'Acura Wagon';
Output: Mercedes E350 Wagon & Audi A4 AllRoad Wagon
$cars =array(0 => "VW Bug",
1 => "VW Thing",
2 => "VW Scirocco",
3 => "VW Golf GTI",
4 => "VW Bus w 27 Windows" );
Output: VW Scirocco
Tough stuff? No, nice work. Keep at it, it will come to you. It takes time, please be aware of the syntax, how the code is written. This is key when you are troubleshooting code, I can't stress this enough, devs have to make sure structure is right, brackets and semicolons ; are placed correctly.
Example 17 - PHP Assocative Arrays - descriptive names are used for id keys:
Array ( [John] => Male, 43 [Luke] => Male, 32 )
Output: John is a Male, 43
Example 18 - PHP Multi-Demensional Arrays - Nested arrays:
Array ( [comedy] => Array (  => Pink Panther  => John English  => See No Evil Hear No Evil ) [action] => Array (  => Die Hard  => Die Hard 2  => Expendables ) [epic] => Array (  => The Lord Of The Rings  => Harry Potter ) [romance] => Array (  => Romeo and Juliet  => Sleepless In Seattle ) )
Output: My favorite movie: Harry Potter
Example 19 - PHP Arrays: Operators
x + y - Union, combines elements from arrays
Array ( [id] => 1 [value] => 10 )
x == y - Equal, if arrays are equal, return true if yes
Output: true or 0
x === y - identical, values/data types
Output: false or 0
x != y - Not identical
Output: true or 0
x !== y - Not identical
Output: true or 1
Example 20 - PHP Arrays: Functions - Count:
Example 21 - PHP Arrays: Functions - is_array (is in the array?):
Example 22 - PHP Arrays: Functions - sort:
Alphanumeric, alphabetical order
Numeric, Ascending order
Removes existing keys and ad new numeric key
Sort teachers in order, this will be alphabetical
Output: Array (  => Female  => Male  => Male  => Male )
Example 23 - PHP Arrays: Functions - ksort or using key to sort:
KSort teachers in order, this will be alphabetical by key
Output: Array ( [April Apple] => Female [Mr.Held] => Male [Mr.Hill] => Male [Mr.Jones] => Male )
Example 24 - PHP Arrays: Functions - asort or using array to sort:
ASort teachers in order, sort array using the values
Output: Array ( [Marne] => Female [Mirriam] => Female [John] => Male )
Need help with other front-end code, jQuery, Bootstrap, JS? We are diligently working behind the scenes to bring our learning platform to the masses. Scripting and/or Object-Oriented languages and more! Check out the main topics contained in this website.