PHP's syntax and semantics are similar to most other programming languages (C, Java, Perl)
with the addition that all PHP code is contained with a tag, of sorts. All
PHP code must be contained within the following...
PHP Code:
<?php ?> or the shorthand PHP tag that requires shorthand support to be enabled on your server... <? ?>
If you are writing PHP scripts and plan on distributing them, we suggest
that you use the standard form (which includes the ?php) rather than the shorthand
form. This will ensure that your scripts will work, even when running on other
servers with different settings.
How to Save Your PHP Pages
If you have PHP inserted into your HTML and want theweb browser to interpret it correctly, then you must save the file with a .php extension,
instead of the standard .html extension. So be sure to check that you are saving your files
correctly. Instead of index.html, it should be index.php if there
is PHP code in the file.
Example Simple HTML & PHP Page
Below is an example of one of the easiest PHP and HTML page that youcan create and still follow web standards.
PHP and HTML Code:
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Display:
Hello World!
If you save this file (e.g. helloworld.php) and place it on PHP enabled server and load it up in your web browser, then you should see
"Hello World!" displayed. If not, please check that you followed our example
correctly.
We used the PHP command echo to write "Hello World!" and we will be talking in greater depth about how echo is special later on in this tutorial.
The Semicolon!
As you may or may not have noticed in the above example, there was a semicolon afterthe line of PHP code. The semicolon signifies the end of a PHP statement and
should never be forgotten. For example, if we repeated our "Hello World!" code several times, then we would
need to place a semicolon at the end of each statement.
PHP and HTML Code:
<html> <head> <title>My First PHP Page</title> </head> <body> <?php echo "Hello World! "; echo "Hello World! "; echo "Hello World! "; echo "Hello World! "; echo "Hello World! "; ?> </body> </html>
Display:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
White Space
As with HTML, whitespace is ignored between PHP statements. This means it is OK to haveone line of PHP code, then 20 lines of blank space before the next line of PHP code. You can also
press tab to indent your code and the PHP interpreter will ignore those spaces as well.
PHP and HTML Code:
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
echo "Hello World!";
?>
</body>
</html>
Display:
Hello World!Hello World!
PHP Variables :
virtualinfocom way
A variable is a means of storing a value, such as text string "Hello World!"or the integer value 4. A variable can then be reused throughout
your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:
- $variable_name = Value;
If you forget that dollar sign at the beginning, it will not work. This is a common
mistake for new PHP programmers!
Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes.
A Quick Variable Example
Say that we wanted to store the values that we talked about in the above paragraph.How would we go about doing this? We would first want to make a variable name and then
set that equal to the value we want. See our example below for the correct way to do this.
PHP Code:
<?php $hello = "Hello World!"; $a_number = 4; $anotherNumber = 8; ?>
being initialized.
PHP Variable Naming Conventions
There are a few rules that you need to follow when choosing a name for yourPHP variables.
- PHP variables must start with a letter or underscore "_".
- PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
- Variables with more than one word should be separated with underscores. $my_variable
- Variables with more than one word can also be distinguished with capitalization. $myVariable
Outputting a String
To output a string, like we have done in previous lessons, use PHP echo. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.PHP Code:
<?php $myString = "Hello!"; echo $myString; echo "<h5>I love using PHP!</h5>"; ?>
Display:
Hello!
In the above example we output "Hello!" without a hitch. The text we areI love using PHP!
outputting is being sent to the user in the form of a web page, so it is important that we use proper
HTML syntax!
In our second echo statement we use echo to write a valid Header 5 HTML statement.
To do this we simply put the <h5> at the beginning of the string and closed it at the end of the
string. Just because you're using PHP to make web pages does not mean you can forget
about HTML syntax!
Careful When Echoing Quotes!
It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! Echo uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:- Don't use quotes inside your string
- Escape your quotes that are within the string with a backslash. To escape a quote
just place a backslash directly before the quotation mark, i.e. \" - Use single quotes (apostrophes) for quotes inside your string.
See our example below for the right and wrong use of echo:
PHP Code:
<?php // This won't work because of the quotes around specialH5! echo "<h5 class="specialH5">I love using PHP!</h5>"; // OK because we escaped the quotes! echo "<h5 class=\"specialH5\">I love using PHP!</h5>"; // OK because we used an apostrophe ' echo "<h5 class='specialH5'>I love using PHP!</h5>"; ?>
If you want to output a string that includes quotations, either use an apostrophe ( ' ) or escape the
quotations by placing a backslash in front of it ( \" ). The backslash will tell PHP that you want the quotation
to be used within the string and NOT to be used to end echo's string.
Echoing Variables
Echoing variables is very easy. The PHP developers put in some extra workto make the common task of echoing all variables nearly foolproof! No quotations are
required, even if the variable does not hold a string. Below is the correct
format for echoing a variable.
PHP Code:
<?php $my_string = "Hello Bob. My name is: "; $my_number = 4; $my_letter = a; echo $my_string; echo $my_number; echo $my_letter; ?>
Display:
Hello Bob. My name is: 4a
Echoing Variables and Text Strings
You can also place variables inside of double-quoted strings (e.g. "string here and a $variable"). By putting a variable inside the quotes (" ") you are telling PHP that you want it to grab the string value of that variable and use it in the string. The example below shows an example of this cool feature.PHP Code:
<?php $my_string = "Hello Bob. My name is: "; echo "$my_string Bobettta <br />"; echo "Hi, I'm Bob. Who are you? $my_string <br />"; echo "Hi, I'm Bob. Who are you? $my_string Bobetta"; ?>
Display:
Hello Bob. My name is: Bobetta
Hi, I'm Bob. Who are you? Hello Bob. My name is:
Hi, I'm Bob. Who are you? Hello Bob. My name is: Bobetta
By placing variables inside a string you can save yourself some time and make your code easier to read, though it does take some getting used to. Remember to use double-quotes, single-quotes will not grab the value of the string. Single-quotes will just output the variable name to the string, like )$my_string), rather than (Hello Bob. My name is: ).
Hi, I'm Bob. Who are you? Hello Bob. My name is:
Hi, I'm Bob. Who are you? Hello Bob. My name is: Bobetta
Comments (0)
Post a Comment