Join David Powers for an in-depth discussion in this video Displaying text and numbers, part of Introducing PHP.
- View Offline
- Let's start writing some PHP code to store numbers and text in variables. And then display their values in the browser. I'll also show you what happens if you use illegal names for variables. Open your Script Editor and create a PHP file. I've called mine display_values.php We don't need any html code in this file, so if your editing program has inserted html you can delete it. But the file must have an opening PHP tag.
We're only going to be using PHP, so there's no need for a closing PHP tag. So let's create a variable. Variables always begin with a dollar sign followed by the name. I'm going to call this first name and I'm using camel case, so an uppercase n for name. We need to give the variable a value and the value can come from a variety of sources. It can come from user input from an online form, from a database, from an external source such as a newsfeed or adjacent, or from the PHP file itself.
But wherever the value comes from, you assign it to the variable with the assignment operator, which is simply an equal sign. First name is going to be text and text needs to be in quotes. In PHP it doesn't really matter whether you use single or double quotes. There is a subtle difference between them, which I'll explain later in the course. But for now, I'm just going to use single quotes and between those quotes put my name David and after the closing quote we need a semicolon.
So we can duplicate this line and then assign a different value to it. Let's assign a number this time and numbers are not in quotes, so I've now reassigned 42 to firstName. So as well as changing the value, I've also changed the data type. But that's not really a very good idea and firstName isn't a very meaningful variable to store a number. So I'm going to change that to number. So we've now got two variables, firstName and number.
Numbers can contain the decimal point, so we could change this to 42.99 The decimal point must be a period or a dot. You can't use a comma for the decimal point, as is common practice in many European countries. Also, you can't use the comma as the thousand separator. So if I change this to 4200 and then try to put in a thousand separator a comma, what happens in my editing program is that I get this wavy red line indicating that I've got a syntax error.
So I need to get rid of that comma. No thousand separators in numbers. So let's just save that, we've got our two variables now. And try to load this into a browser. And although we've loaded the page into a browser, nothing happens. To display the value of a variable that contains a string or a number, you need to use echo or print. So let's go back to the editing program and I'm going to use echo. Very often you'll see that people put parentheses after echo and then they put the value that they want to display between the parentheses.
Using parentheses with echo is completely optional. It only makes for extra typing, so I don't think it's worthwhile doing. So I'm just going to have my variable firstName straight after that and make sure that we put in that semicolon at the end and save the page. And refresh the browser. And this time, it displays the value that's stored in firstName. In other words, David. So let's see what happens with print. Again with print, you'll often see people use parentheses and put the value they want to display between the parentheses.
Just as with echo, the parentheses are optional so I'm not going to use them. This time I'll display number, put that semicolon on the end, save and refresh. And it's doing exactly the same, it's displaying the number straight after the value of firstName. The main difference between print and echo is that print can display only a single value, whereas echo can display a series of values separated by commas.
So what I'm going to do is I'm going to come back into this editing program here and at the beginning of line six I'm going to insert a couple of forward slashes. That disables for line, this is what's known as commenting out of line. We'll be discussing comments in the next video. What I want to do now is to add extra values to echo separated by commas. So I'm going to put in number first, followed by a comma. You could also have literal text or a literal string.
So I'm going to have a pair of single quotes with a space between and then a comma. So echo is going to display number, a space, and firstName. And if we save that and reload the page in the browser, Indeed we've got the number, we've got a space and the value of firstName. So that's basically all that you need to do to store text and numbers and variables and then display their values using echo and print.
But let's see what happens if you use illegal characters in your variable names. I'm going to come here and duplicate that line, firstName. And instead of spelling out firstName, I'm going to use the abbreviation 1st. And as soon as I do that, my editing program objects, it says you're not allowed to do that. But I'm still going to do it and I'm going to save and then reload the page in the browser. And even though I'm not attempting to display that new variable, I get a pause error.
Nothing is displayed in the page, except this error message and it says syntax error unexpected '1'. We can't have that number at the beginning. Now if you're testing this yourself and you've got a blank screen, you need to set display_errors to on as described in chapter one. So obviously, starting with a number after the dollar sign that won't work. So let's put two slashes at the beginning to comment that there, then let's just duplicate again and this time we'll try a hyphen in the middle there.
And if we save that, I'm not even trying to display it I'm just going to refresh the browser. Again I get a syntax error, you cannot have a hyphen in the middle of a variable name. So again, comment that line out and let's just see what happens if I change firstName here to firstName with a lowercase n, save that and refresh in the browser. It displays the number and then we get this notice undefined variable firstname.
It's very important, variables are case sensitive in PHP. We must have that uppercase n to be able to display firstName correctly. The only characters that are permitted after the dollar sign in variable names are letters, numbers, and the underscore. But letters means not only the 26 letters of the Roman alphabet. It also includes accented characters used in Western European languages. So if French is your first language, instead of firstName, you could use prenom.
So let's just try that. I'm going to change firstName here to prenom. And then I'll comment that line out and we'll use echo at the end with our new French variable prenom. Put that semicolon on the end, save and reload the page in the browser. And it has displayed the value of prenom. So that concludes this basic introduction to storing text and numbers in variables.
And then displaying their values using echo and print. Although echo and print perform essentially the same task, my preference is to use echo without parentheses. It's less typing and it does the job equally well.
- Naming variables
- Storing text as strings
- Doing calculations with PHP
- Using conditional statements to make decisions
- Creating custom functions
- Deciphering error messages
- Emailing the contents of an online form
- Dealing with multiple-choice form fields