Save and open the file in the browser. Go to File > Save As… and save the file as "helloworld2.php', and open it in your browser by using the address: http://localhost/helloworld2.php. The output is the same as before, but this time the text is in bold.
Make sure you save the file to your 'server's' document root directory. Typically this is the folder named 'htdocs' in your Apache folder on Windows, or /Library/Webserver/Documents on OSX, but can be set by the user manually.
How to Write PHP Scripts Picture 9
Edit the file to add a second echo statement. Remember, statements need to be separated by a semicolon.
Your code should look something like:
How to Write PHP Scripts Picture 10
Save and run the file as "hello world double.php". The page will display two echo statements, listed in order, on two lines. Notice the ' ' on the first line. This is HTML markup to insert a line break.
If you didn't add this, your output would look like this: Hello World!How are you doing?
Part 3 of 3:
Getting to Know Variables
How to Write PHP Scripts Picture 11
Think of variables as containers for data. To manipulate data, be it numbers or names, you need to store the data in a container. This process is called declaring the variable. The syntax for declaring a variable is '$myVariable = 'Hello World!';'
The dollar sign ($) at the beginning tells PHP that $myVariable is a variable. All variables must start with the dollar sign, but the name of the variable can be anything.
In the above example, the value is "Hello World!", and the variable is $myVariable. You're telling PHP to store the value at the right of the equal sign, into the variable at the left of the equal sign.
A variable containing a text value is known as a string.
How to Write PHP Scripts Picture 12
Call the variable. Referring to a variable in the code is known as a call. Declare your variable, then echo the variable instead of typing out the text.
Your code might look something like:
How to Write PHP Scripts Picture 13
Save and run the file. Go to File > Save As… and save the file as 'myfirstvariable.php'. Open your browser and navigate to http://localhost/myfirstvariable.php and the script will print the variable. The output looks the same as printing plain text, but how it was achieved is different.
Make sure you save the file to your 'server's' document root directory. Typically this is the folder named 'htdocs' in your Apache folder on Windows, or /Library/Webserver/Documents on OSX, but can be set by the user manually.
How to Write PHP Scripts Picture 14
Use variables with numbers. Variables can also contain numbers (known as integers), and then those numbers can be manipulated using simple mathematical functions. Start by declaring three variables called '$mySmallNumber', '$myLargeNumber', and '$myTotal'.
Your code should look something like:
How to Write PHP Scripts Picture 15
Assign integer values to the first two variables. Give an integer value to '$mySmallNumber' and 'myLargeNumber'.
Note that integer values do not need to be contained in quotation marks. That will cause numbers to be treated as a text value like the 'Hello World!' variable.
Your code should look something like:
How to Write PHP Scripts Picture 16
Use the third variable to calculate and print the sum of the other variables. Rather than doing the math yourself, you can call the two variables in the '$myTotal' variable. Using a mathematical function, the machine will calculate the sum for you. To print the variable, you need only add an echo statement that calls the variable after the declaration.
Any change to either integer variable would be reflected when printing the '$myTotal' variable with echo.
Your code should look something like:
How to Write PHP Scripts Picture 17
Save the file and run this script. Your browser window will display a single number. That number is the sum of the two variables called in the '$myTotal' variable.
How to Write PHP Scripts Picture 18
Review your string variables. Using a variable to store text allows you to call that variable any time you want to use the store value instead of constantly typing out the contained text. It also allows for more complex manipulation of the stored data moving forward.
The first variable, $myVariable, contains a string value; "Hello World!". Unless you change the value, $myVariable will always contain the value "Hello World!".
The echo statement prints the contained value of $myVariable.
How to Write PHP Scripts Picture 19
Review your integer variables. You have explored basic manipulation of integer variables by using a mathematical function. The resulting data can be stored into another variable. This is only the beginning of what can be accomplished with these variables.
The two variables, $mySmallNumber, and $myLargeNumber are each assigned an integer value.
The third variable, $myTotal, stores the added values of $mySmallNumber and $myLargeNumber. Since $mySmallNumber holds one numeric value, and $myLargeNumber holds a second numeric value, this means $myTotal holds the value of the first number added to the second number. This value can change with alterations to either of the included variables.
Sample PHP Scripts
How to Write PHP Scripts Picture 20 Sample PHP Echo Template
How to Write PHP Scripts Picture 21 Sample PHP Variable with Words
How to Write PHP Scripts Picture 22 Sample PHP Variable with Numbers