1. Base
A language as old as the internet itself. The first one that made pages go from static to dynamic. But do not make the mistake of thinking it is obsolete or old: it is more alive than ever!. It has known how to adapt to the new times: object orientation, libraries, image handling, etc. Today it is so stable that it is used by large sites like Wikipedia and Facebook. On top of that, major CMS platforms, such as WordPress, are written in this language.
I offer you a free PHP course (version 8.x). You will learn the most modern techniques for backend development and the foundations for anyone who wants to create dynamic pages. If you have any question during the lessons, or think a section should be expanded, do not hesitate to leave a comment at the bottom of the page.
Creating files
PHP runs in a plain file. To do this you must create a document with the .php extension and put your HTML inside it.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
</body>
</html>
For now you have no reason to be scared. It is practically the same as making a static HTML page, except that we have changed the extension.
To add your PHP scripts you must use <?php ?>. There are also short tags <? ?>, but they are discouraged and you should not use them, since they may be disabled on the server and cause compatibility problems.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
<?php echo 'And it works perfectly!'; ?>
</body>
</html>
Every statement, the instruction, must end with a ;. Regardless of where the script is opened or closed. You are free to add tabs and line breaks to keep it tidier.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
<?php
echo 'And it works perfectly!';
?>
</body>
</html>
There is a very simple shortcut for
<?php echo 'Hello World'; ?>:<?= 'Hello World' ?>. It will be used very little during the course.
Spinning up a PHP server quickly
If you try to open the file you will notice that nothing happened. We need to process the PHP file so that the final HTML is displayed.
PHP's mission is to generate HTML, the browser does not understand PHP. Every PHP file must be run with some software.
If you have PHP installed on your operating system, and know how to work with the terminal, you can quickly run the built-in internal server it includes.
php -S localhost:8000
And open, in your favorite browser, localhost:8000.
If you are not that much of a geek, I recommend the way 99.9% of people use. Install some software.
On Windows: XAMPP.
On Mac OS: MAMP.
On Linux: Nginx + PHP or XAMPP.
After installing it, a folder named htdocs will appear where you will place your PHP assets.
If you have doubts about the process, you can look up more information on Google. In the course I am going to focus on topics related to the language, not software.
Printing
The reserved word echo is used to print to the screen. That means that any text we put after echo will appear in the place where the script was declared.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
<?php
echo '<p>And it works perfectly!</p>';
?>
</body>
</html>
It will end up being.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
<p>And it works perfectly!</p>
</body>
</html>
If you want to output several lines you have several options.
The simplest is using several echos.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
<?php
echo '<p>In a village of La Mancha,</p>';
echo '<p>the name of which I have no desire to call to mind...</p>';
?>
</body>
</html>
Another is adding a line break represented as PHP_EOL.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
<?php
echo '<p>In a village of La Mancha,</p>' . PHP_EOL . '<p>the name of which I have no desire to call to mind...</p>';
?>
</body>
</html>
On the internet you will see thousands of examples where
\nis used for line breaks. If you use it you could run into compatibility problems between operating systems, since the way to make a line break varies on each one.PHP_EOLmakes your life easier since it automatically picks the correct one.
And the last one is using <<<. Remember to put END; at the beginning of the line.
<html>
<head></head>
<body>
<h1>My first PHP file</h1>
<?php
echo <<<END
In a village of La Mancha,
the name of which I have no desire to call to mind...
END;
?>
</body>
</html>
Concatenating
You must use a dot between the two texts. The spaces are optional, but they will help you visualize it more easily.
<html>
<head></head>
<body>
<h1>Two Beatles</h1>
<?php
echo 'John Lennon' . ' and ' . 'Paul McCartney';
?>
</body>
</html>
Comments
There are two types of comments: line and multiline.
// This is a single-line comment
echo 'This is a test';
/* This is a multiline comment
and another line of comments */
echo 'This is another test';
echo 'A final test';
# This is a console-style single-line comment
The single-line ones start with // or #, followed by a space.
Meanwhile the multiline ones start with /* and end with */.
If you want to leave well-delimited and tidy sections J. Prettyman proposes the following structure.
//======================================================================
// CATEGORY IN UPPERCASE
//======================================================================
//-----------------------------------------------------
// Sub-Category In Lowercase
//-----------------------------------------------------
/* Title with Capital Letters */
# Option 1
# Option 2
# Option 3
/*
* Long explanation of some
* functionality or block of code
* that deserves it.
*/
// Note
Activity 1
Build a simple calculator.
- Put a number in a variable.
- Put a number in another variable.
- Add them and show the result.
Pro:
- Perform other operations (Subtract, divide...).
Activity 2
- Save the name of a girl in a variable.
- Save the name of a boy in another variable.
- Show the sentence> "{girl} likes {boy}."
For example: Sonia likes Roberto.
Pro:
Save a name and a year of birth in two variables. Show the following sentence calculating the age: "My name is {name} and I was born in {year of birth}. Therefore I am 23."
Activity 3
- Save a number.
- Calculate the VAT of that figure.
- Show the same figure with VAT added.
Hint: To calculate the VAT you must apply the following formula price * 1.21.
Pro:
- Also show the figure without VAT.
- Be creative! Format the result in an attractive way.
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
Desafíos de programación atemporales y multiparadigmáticos
Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.
Buy the bookWill you buy me a coffee?
This is how I keep writing without ads or paywalls.
Sure, it's on me!
Comments
There are no comments yet.