Welcome everyone to the Day # 1 of my PHP tutorials series, Learn PHP in 7 days. Today we are going to start with basic concepts of PHP like the syntax, using variables, strings and the printing output on the browser using echo statement. Without wasting your time Iâ€™ll start the tutorial right away.
Note that every statement in PHP finishes with a semicolon ” ; ” at the end except for looping structures. First I may tell you how PHP works. Working with PHP files is the same way like you work with HTML files, but these files end with a .php extension rather than .htm or .html that we usually use for HTML files.
PHP file is nothing but an HTML file. That means you can write an execute HTML inside a PHP file. Thatâ€™s for sure but how on earth do we execute PHP code? Thatâ€™s what we expect to learn here right?
Anything placed inside the tags (or a PHP Block) will be you PHP code. For instance:
The above statement is going to output like this on the web browser.
So this is how you get your output. We execute PHP code in a PHP Block and its gets compiled and is displayed on your browser.
Example of a PHP and HTML Code
My First PHP Page
<?php echo "Hello World!";
I remind you once again, every statement in PHP finishes with a semicolon ” ; ” at the end except for looping structures.
White space between 2 php lines is ignored, so that means you can leave white space without any problem
A variable is something that can store changing values. If you already know any other programming language or probably Algebra, then you already know what a variable is. The concept of variables is beyond this tutorial so I assume that you already know about a variable.
Variables in PHP are stored with a dollar sign ” $ ” before the variable name. There are some naming conventions for creating variables.
PHP variables can start only with a letter or underscore “_”
PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _
$myname = "Atif";
$myfullname = 'Mohd Ameenuddin Atif';
$number = 5;
$decimal = 12.25;
As the above code is pretty self explanatory, any string variable must be wrapped inside single (â€™) or double (“) quotes where as any integer or a floating point (decimal) number should be written straight away.
Let us start with a “hello world” example. Coming back to our hello world example, create a new file using your favorite text editor. Writing hefty applications in a text editor is quite hard to debug. Hence I would recommend you to get a copy of RapidPHP , that is what I use for my self.
Write the following code into the file
echo ("Hello, World!" );
Now save it as lesson1.php or whatever you would like. Make sure it resides inside the www folder of your WampServer Directory. Open its relative URL like http://localhost/lesson1.php to make sure it works.
Ahh! Great It works!
Echo is a language construct that allows you to print any thing on the screen. The PHP command echo is a means of outputting text to the web browser. You can print any HTML using echo statement. Here are a few examples of using echo statement to output plain text
<?php I am learning PHP"; ?>
<?php HTML Title tag using PHP "; ?>
And this is how we echo a variable on the web browser.
<?php $websitename = "MyGeekPal!";
<?php echo "hi";
<?php echo "Welcome to $websitename"; ?>
This way the browser will echo the string along with the variable.
Make a note that the variable will be parsed only if its supplied within the double quotes.
An alternative of writing this would be the following way
$websitename = "MyGeekPal!";
echo "<h5>Welcome to " . $websitename . "</h5>";
In both the ways the output would be virtually same but when we take it in terms of CPU usage, the output will be processed faster in the second method.
The ( . ) or dot operator is used to simply join 2 expressions. The output you get is
How to echo quotes
To echo quotes or other extend characters like line breaks we use backslash ( ) before the character or also called as escaping text. Let us see an example
Here is the output
To echo a backslash, write the backslash twice ( \ )
Heredoc method is another robust method in PHP that can help you create multi line strings variables without using escape characters. Let me first write the code and then explain it to you.
$a_string = <<<TEST
Testing PHP Heredoc
Use <<< and some identifier keyword (my suggestion would be to use some word that is no where used inside the variable.) like we used TEST.
Start writing the multi line string that you want the variable to hold from the next line onwards.
To close it, simply write the name of the identifier in the next line. Be warned that the line closing the variable must contain only the identifier that we used to start it followed by a semicolon and the line must not be indented.
This is the output that you would get
- Sanitizing Variables in PHP without Regular Expressions
- Learn PHP in 7 Days
- MS DOS Tutorials – How to Create, Append and View Files and Move Files and Directory
- How to Create a WordPress Plugin – Hello World!
- MS DOS Tutorials – How to Create, Delete, Change, Rename Directory and Files