In this article I’m gonna show you how to code with PHP. In order to do that, you’d need to install XAMPP first. Keep reading for instructions on how to install XAMPP.
Download and install XAMPP at https://www.apachefriends.org/index.html.
During installation, you can un-check “Find out more about Bitnami”, since it’s not going to be used.
After installation, start XAMPP, and in your browser go to localhost. You should see a welcome page from XAMPP.
PHP language structure
PHP is a procedural language, which means that code is interpreted/executed line by line.
A PHP file ends with “.php”. When a PHP file is requested, the server reads the file, finds the PHP code blocks, execute them, then replacing the original code with the results. That being said, PHP code will not be delivered to the client, only its results will be.
A PHP block code must start with “<?php” and end with “?>”.
A code line in a PHP block must end with a semicolon “;”.
Let’s do our first demo!
- Go to your XAMPP’s “localhost” folder (usually the in C:/XAMPP/htdocs).
- Create a new file, name it “index.php”
- Type in the following lines of code
4. Save the file.
5. Open your browser and go to your file in “localhost”, you should see this:
As you can see above, we have some regular HTML elements, such as <head>, <body>, <div>. Now pay attention to the <h1> element, and the <div> element after that. Both of them have PHP code blocks in them, signified by the opening “<?php” and closing “?>”. Each PHP code block basically does one thing: prints something out onto the page. That’s what “echo” does, “echo” is a function to print out string onto the page. For <h1> it’s “First div here”, and for the ensuing <div> it’s to print out “And this is the second div!”.
Now let’s play with variables.
In PHP, there is no variable declaration, and thus no type association, like Java.
A variable must start with a dollar sign “$”. To use/assign a value to a variable, we simply assign the value, like this: $example=1. The “$example” will now carry the integer value of 1.
In your index.php file, type in the following content (or you can create another PHP file if you wish, just remember to point to the correct file on your “localhost”). Remember to add the opening and closing tags for PHP code block.
Before I explain the code, I’m gonna draw your attention to the lines starting with double slashes “//”. Anything that goes after “//” is a comment, and will be ignored by the PHP engine. Comment is used for your own convenience, i.e noting complex algorithms, remind yourself to code something.
So in the example, we have “$example=1”. This line creates the “$example” variable (since it doesn’t already exist), and assign an integer value of 1 to it. After that we just “echo” it onto the page.
To demonstrate “type juggling” — the idea of a variable being able to hold values of different types, I assigned another value of type String, not Integer, to the “$example” variable. And then “echo” it onto the page.
Then in the same file, I want you to append the following lines of code:
What special about this is, “$$example” is called, “$example” part is substituted with “variable”, since “$example” carries the string “variable”. After the substitution, “$$example” would be “$variable”, and ta-da, “$variable” holds the string “I’m the value”. Think of it as a double variable call, something special you can do with PHP.
After all that, open your browser and point to the file in your “localhost”. You should see this:
I believe you are familiar with boolean, integer, float number, string, array, object, and NULL. Resource is the odd one here since we don’t have it in Java. Resource just basically refers to another external resource, like an image, a video clip, or a database connection.
Assigning values of boolean, integer, float number, and string type is pretty straight-forward, like in Java, we just assign it using a equal sign “=”. Therefore, I’m going to touch on array type and resource type.
We have 2 types of array, and a hybrid that uses both.
In your index.php type in the following lines of code:
We’re basically creating a new array, and giving it 5 strings: “dog”, “cat”, “tarantula”, “dolphin”, “goldfish”. Each strings will automatically be assigned an index, starting from 0. This means “goldfish” will have index of 4.
Then run the file in your browser, you should see this:
In your PHP file type in the following:
As you noticed, declaring the array this way will allow us to point to the value through its key, rather than its index. The key “second” holds the value “second value”. This is similar to Java’s associative array, or a Hash Map.
Run the code above and you should see:
Or, you can combine the use of the 2 arrays, like this:
The left side is the code, and the right side is the result of running the code through the XAMPP server. Try it with your computer!
In the example above, we see that the “$anotherArray” is both indexed by 0, and accessed through the “second” key.
As mentioned above, a resource can be an image. So we’re going to try creating an image from scratch.
In your PHP file type the following code lines:
Let’s take a closer look at the code:
- header(‘Content-Type: image/png’): this specifies what type of content we’re returning. Usually it’s an HTML page. However, since we’re returning an image, we have to tell the browser that, so that it can display the image correctly.
- imagecreateturecolor(200, 200): creating a black canvas of size 200x200.
- Then we save the image resource in “$image”.
- imagecolorallocate method returns an integer that represent the color red.
- Then we write a string “A string written across” on the image with the method “imagestring”.
- Then we output the image onto the page to return to the browser.
Run the code and you should see this image in your browser:
A very important structure of any programming language is the looping structure. PHP has some that you can use.
For and foreach loop
In your PHP file, type the following:
Let’s break down the code:
- The “$myArray” is first declared as an indexed array.
- In the “foreach” loop, each element in the “$myArray” is casted to the “$animal” variable, then “$animal” is printed out on to the page.
- Then, we have a separate “for” loop to print out each “$i”, with “$i” running from 10 down to 0.
Run the code and you should see this:
_GET & _POST variables
In Java, when we want to retrieve GET or POST variables, we’d get the request variable first, then get the attributes from the request. In PHP, you can directly get the variables, like this: $_GET[<variable name>] or $_POST[<variable name>]
Run either of them, enter a value in the “Username” field and submit. You should see the username value displayed on the page. The “Password” field is not displayed, so you can go ahead, retrieve and try to display it!
In PHP, creating and ending a session is rather simple, since the language is tailored specifically for writing websites.
To create a new session, we do:
- Call method session_start() to start the session.
- Set session variable $_SESSION[<variable name>]
To end a session securely, we do:
- session_unset() to clear all session attributes.
- session_destroy() to completely delete the session information from the server.
To try out session, code the following:
On the right side is the result that you should see. Modify the session attributes to better understand how it works!
Above are all the basic concepts of PHP, with which you can get started & create your very first dynamic PHP page. Anything else is just a Google away. Happy coding!