One of the very first things you are going to want to do is learn where are the php operators! Learn this well, Google
In the Google Results Page, look for the website:
And click there!
You should find yourself On the Operators page of the PHP Manual.
You will be looking at a list of links to the various PHP Operators.
In the Beginning…
As you start to learn php, you will use this page a lot. Operators are used in most lines of code. I tell you this so you will take note of how to get back to the Operators page, because you will be coming back here (after a decade and a half of coding php, I still come back here some days!).
Assigning a Simple Variable
There is a basic assignment operator, it is “=” (the “equal sign” without the quote marks!). It can be modified a number of ways with other operators, but we will leave that for another lesson.
You may be tempted to think of = as being “equals”, but that is very dangerous in conditions! What you should do is think of this assignment operator as “gets set to” instead of “equals”. When you get to conditional operators you will see that == means “equals”, so better get used to “=” meaning “gets set to” from the start!
My apprentice name is Marie Estebanie Pelin, so lets set her name in a variable name $name.
<?php $name = 'marie estebanie pelin'; ?>
Remember to read this as:
$name gets set to 'marie estebanie pelin';
Always Use Single Quotes…
A good rule I live by is, “Always use single quotes, unless you MUST use double quotes!” It will save you in debugging time! If you do not know what to use, it is a safe bet you likely need single quotes.
For this I am using The real HTML5 boilerplate, so the HTML5 looks like this…
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>HTML5 boilerplate – all you really need…</title> <link rel="stylesheet" href="css/style.css"> <!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body id="home"> <h1>HTML5 boilerplate</h1> </body> </html>
Inserting PHP into HTML
Now let’s add the PHP so that both the title and the body <h1>tag display Marie’s name.
<?php // marie_name.php $name = 'marie estebanie pelin'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title><?=$name;?></title> <link rel="stylesheet" href="css/style.css"> <!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body id="home"> <h1><?php echo $name; ?></h1> </body> </html>
After PHP v5.4 the following two commands are identical. This is because the echo shorthand has many uses. Before v5.4 you should make sure short-tags are turned on in PHP. If you are not sure, the one with the word echo in it is likely safer at this time. But as servers get updated to v5.4 the shorter one will see more and more usage. They both work fine on most systems now.
<?=$name;?> <?php echo $name; ?>
Either one echos (outputs) the data within the Variable.
In this lesson you learned how to put a literal string into a Variable and how to late echo it back to the browser screen.