Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
While most of this video series deals with PHP code that's generated by Dreamweaver, it's useful to know a little bit about hand-coding PHP. So I'm going to show you how to set a simple variable, and then how to output the value of that variable in a web page. I'll be working with files in the folder 02_gettingstarted > 05_variables, and I'll open the file in that folder, variables.php. Let's take a look at the existing code. In this file, there is just simple use of the echo command that's outputting the literal string Hello World from PHP.
I'm going to show you how to set a simple variable that contains that same value. A variable is like a little bucket of information. All programming languages support them, but the way you declare and set the value of a variable in each programming language differs. You can set a variable anywhere in a PHP page, but wherever you set it, the variable will only be available from that point downward. So I'm going to place the cursor at the top of my page in Code View and then press Enter or Return a couple of times to make some blank space.
And then I'll move the cursor up to the top of the page. Then I'll go to my Insert panel, and I'll choose the PHP category, and then I'll choose Code Block. And that adds a simple PHP code block. Then I'll press Enter a couple of times to make a blank line, and within the PHP code block, I'll set a simple variable, named Hello World. You can name your variables anything you want, but you should always prefix them with the Dollar sign. Notice when I type the Dollar sign into Dreamweaver CS5 that I get a list of a number of existing variable that are always a part of the PHP environment, such as COOKIE, ENV, FILES, GET, POST, and so on.
We'll get to some of these later. For now, I'm creating my own variable, and I'll name it welcome. I'll set the value of the variable to "Welcome to my homepage," and I'll end the declaration with a semicolon. Notice as I type the Dreamweaver CS5 checks for syntax errors, and if the page is clean, it shows me the message, No syntax errors. If I remove something from the statement that makes it syntactically incorrect, Dreamweaver immediately shows that there is a syntax error.
I'll put that code back in, and Dreamweaver removes the error message. To use that variable, I'll go down to the echo command where I'm outputting a literal string, and I'll remove the literal string, I'll type the Dollar sign and then a W, and Dreamweaver CS5 shows me my variable, $welcome. This is a new feature of Dreamweaver, the ability to give you hints as to the names of variables, functions, and other elements of your preexisting PHP code.
I'll press Enter or Return, or click with the mouse, and that inserts the
variable into my echo command.
I'll save the changes, and then I'll use the external browser to look at the result.
The value of the welcome variable is now output on the page, and because I
placed the echo command inside
tags, the text shows up as a title one.
I'll View the Selection Source in Firefox, and I'll see that the value that's
output to the browser is the simple text, because once again, the browser is not
capable of interpreting and executing PHP code.
So that's a look at how to set variables using simple assignments statements, and how to output the values of those variables using the echo command. You wrap both statements inside PHP code blocks and then all of the processing of the PHP commands happens on the server, and the resulting HTML markup is returned to the browser.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98382 Viewers
61 Video lessons · 85669 Viewers
71 Video lessons · 69603 Viewers
56 Video lessons · 101928 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.