This week, in my opinion, we start the really interesting stuff: we learn about Server-side development.
Most server-side languages have similarities between them but I will use PHP to teach this class and so, this introduction is geared toward PHP but many concepts can be reused for other languages.
PHP is an typically interpreted language, meaning that your source code is re-compiled each time it is executed, unlike a compiled language which is pre-compiled (there are cache software to pre-compile PHP).
With an interpreted (or compiled) language, the source code of the program is not the same as the output to the browser, unlike HTML or CSS where the browser sees exactly what is written on the disk.
This means that in a way, we can consider that a .php file is "secured". Users cannot download the file, only it's output. Of course, there are other security considerations, but while anyone can steal your HTML, CSS or Javascript files just by visiting your website, it is typically not possible to do the same with php (or any other server-side language).
A PHP file has 2 types of sections: HTML sections and PHP sections.
At the beginning of your file, you are in HTML mode. To switch to PHP mode, open the tag <? And to return to HTML mode, close the tag with ?>
Some servers require that the first <? Tag be opened like this: <?php to confirm that it is indeed PHP using the <? ?> special tag.
And so, a PHP source file is a text document which contains
instructions, separated by a semi-colon and composed of 8 main types of information:
- Functions
- Constructs / Statements
- Literals
- Operators
- Constants
- Variables
- Comments (which are not really instructions)
- Classes and objects
Yes, we will go briefly over them today and yes, you will be tested on them during the final exam (you did understand there was a final exam, right? Don't worry, you'll have time to study before taking it).
Let's go one by one.
A
function is a collection of instructions performing a specific task. For example, the function "print" outputs texts to the browser. You can call a function by its name followed by parentheses containing the parameters.
For example,
print("Hello World!");
Will call the function "print" and sends it the parameter "Hello World!". Since print sends it's parameter to the browser, it will send Hello World! (without the quotes) to be displayed.
A function can have multiple parameters, separated by commas , A function can also return a value, but we will see that shortly when we talk about variables.
But more importantly, you can define your own functions. For example, you could define a function called "printHelloWorld" as follows:
function printHelloWorld(){
print("Hello World!");
}
For a function to return a value, you simply need to use the
return statement such as:
return 4;
By the way, do you see the curly braces? { and } ? They are used to show scope and delimit group of instructions. In this case, the curly braces delimit what is inside the function and what is outside.
If you have a construct (see below), like if for example, the if only applies to the next instruction, but the curly braces can group a lot of instructions together so that they are executed in sequence if the "if" condition matches.
To call a function, just type its name followed by parantheses with, if there are any, the parameters:
print("Hello World!");
printHelloWorld();
Constructs are a series of
statements which are language instructions that are "built-in" and which are not functions.
For example, if and else are two very important statements used to build a conditional construct. If tests a condition and if that condition is true (and only if it is true), it will execute the next instruction, otherwise it will skip it. Else can be used to execute if the condition was false (and only if it was false).
For example:
If ( GetFunctionResult() ){
echo "GetFunctionResult() returned a true value"
}
else{
echo "GetFunctionResult() returned a false value"
}
Most of them are in the "Control Structure" segment of the PHP manual, such as While, for, foreach, continue, return, etc…
When learning PHP, we need to know at least 90% of the statements to be functional (there are a few, like goto and declare which you do not need or use in ordinary development).
In short, statements are there to let your control how your code "flows". That's why they are called "control structures".
Literals are values that are typed literally into the source code. For example, "Hello World!" in the above example for the function print is a literal. In this case, it is a string literal because string means "string of characters"
If I call the function sleep (which pauses for the specified number of seconds) like this: sleep(4), the 4 is a literal. In this case, it is an int or integer literal, a subset of number literals (which also include floating point literals like 4.2 ).
Literal strings are enclosed by either a pair of single quotes or a pair of double quotes (there are other ways, but these are the basics). Numbers are simply typed as is.
Operators are language features which perform operations. For example, the dot (.) operator is used to join 2 string literals. +, -, /, * and % are operators that perform mathematical functions on literals (the % takes the modulus or remainder of a division). == compares if two values are the same (see below) while === compares if two values are the same AND of the same type ("2" and 2 would no longer be the same).
There are a lot of operators and all of them are punctuation marks (except for and, or, xor, instanceof, clone and new), and that some operators can have different uses depending on context ( + for example is different when used on an array).
constants are a way to replace a literal with another representation. For example, we might replace the 4 in the above sleep example with TIME_TO_CYCLE_THE_DISK so that every time we need to wait for the disk to cycle (whatever that means), we have a clear name for the wait. Also, if the time changes in the future to only 3 seconds, we would have a single place to change the value (in the contact declaration) and the value will automatically change in every place the constant is used.
In this case, you would call
define('TIME_TO_CYCLE_THE_DISK", 4);
Near the top of your php file.
Variables are a little like constants in that they hold values, but unlike constants, the values they hold can actually change during the execution of your script. In PHP, variables are prefixed by a $ sign, and unlike functions are case-sensitive ($test and $Test are different variables).
Like every other language, each variable has a specific type ( a string, an int, etc… ) but in PHP, that type can change depending on the value of the variable.
To create a variable in PHP, simply start using it. For example, you can assign a value using the assignment operator (=) like this:
$test = 3;
You can use a variable everywhere a constant or a literal can be used, such as : $test = $x + $y;
Comments are not instructions but rather text which the PHP interpreter doesn't even see: it's invisible to it. Then why put comments? So that you or other developers can have an idea of what's going on. Comments are used to document your code inside of your code.
There are 2 types of comments in PHP: Block comments and inline comments. Block comments start with /* and end with the first */ found, regardless of if another block was opened: they are not nestable.
Line comments start with a // and end at the end of the line or the PHP block ( they end after a ?> ).
Please note that line comments can also start with a # but a lot of PHP developers have rejected using it.
Finally, just become the PHP interpreter do not see comments doesn't mean that comments are only useful for humans: a few tools use special comments to auto-document code, ofter with a comment block beginning with /** instead of just /*. The second * is ignored by PHP but tells the self-documenting editor to use this comment block.
Let's go over to
classes and
objects. PHP is an object-oriented (OO) language. Sure, unlike JAVA it's not a mandatory OO language in that you can totally ignore the OO aspect, but I think it would be sad to not learn about the beauty of OO.
Classes, the basis of OO programming, are a special construct which group together a series of methods and properties. Methods are functions that belong to a class, and properties are variables that are in every instance of a class (called an object).
If we go back to my previous elevator example, instead of having a series of random functions piled in a PHP file along with various variables to hold the state of each elevator, we can define a class "elevator" which contains a few properties: $CurrentFloor, $Direction, $Destinations, $Mode, $ID, etc… as well as functions "SendtoFloor($floor)" or "GetCurrentDirector()".
But the interesting thing, is that when we use an object (which is stored in a variable) to call a function, that function automatically has access to all of the properties of that object. We do not need to pass them as parameters.
We can even derive from another class, meaning that we create a new class which inherits all of the properties and methods of its
base class but which allows you to add new methods or even
override a method by providing new functionalities. We could define for example a class "ExpressElevator" which overrides the regular Elevator behavior.
BTW, each object (implementation of a class) has a special property called $this which is a variable pointing to itself. TO call one of its own functions, an object can call $this->functionName();
Stateful VS Stateless. When an object remembers the current state of the class and changes over time, we say that this class is stateful, otherwise we say that it is stateless.
Does it change anything in the code? No, but this is an important concept which might be glazed over during the follow training because indeed. It doesn't change anything.
Why PHP is so central and important? Because almost every single server on the planet supports PHP and it is the number one language for the Web.
Finally, the PHP documentation on php.net is excellent, well made and most of the articles on functions have comments with useful usage of the functions which can really help a beginner developer.
Sure, Python, Ruby on Rails and even Perl have some market share, but they are nowhere as popular as PHP.
However, once you know how to develop using one language (in this case PHP), it is much easier to learn another language.
Python for example, was meant to be a more "beautiful" and easy to read alternative to C (on which PHP is based).
They do have in each case some very interesting features that PHP is currently lacking, but there aren't as popular right now. Ruby on Rails is even trademarked by its creator who doesn't allow people to use the Ruby on Rails logo on third-party books.
And so, that's the very basic of PHP. Is it enough for you to start programing in PHP? No, that's why there are multiple courses. But it's the very basic.
In the course syllabus, I had planned to talk about MySQL but I realized while planning this Lecture that MySQL should have its own lecture, so I am changing the schedule and changing the lecture to 11 lectures instead of ten. Next week, we will have Lecture 6: MySQL and not Lecture 5B.
Your assignment for next week ?
Take your current .html file for the previous assignment, and copy it to a .php file. In the source, add a php block and output the current date in text format.
To know how to print the current date, you will need to search the PHP documentation online on the official PHP.net site.
Send me the link via a private message.