Welcome to PHP Tutorial from CodeExercise. I hope you are seen our Best PHP Tutorial For Beginners Part – 1. We are explain PHP – Introduction in forst part.
In the last part of the tutorial We explained a number of the advantages of PHP as a scripting language and showed you how to check your server for PHP. in this part i’ll show you the fundamentals of showing information within the browser and how you’ll use variables to hold data.
To output text in your PHP script is actually very easy. As with most other things in PHP, you can do it in a variety of different ways. The main one you will be using, though, is print. Print will allow you to output text, variables or a combination of the two so that they display on the screen.
The print (echo) statement is used in the following way:
print("Hello Sir!"); or echo("hi Mam / Madam");
The print (echo) Full PHP and HTML. PHP code can be written right into your HTML, like this:
<!DOCTYPE html> <html> <head> </head> <body> <p> <?php echo "My first line of PHP!"; print "My 2nd line of PHP!"; ?> </p> </body> </html>
I Will Explain The Above Line:
Your PHP code goes inside the <?php and ?> delimiters. Here we use the function echo / print to output I’m learning PHP!. We also end the line with a semicolon.
Finally, as with nearly every line in a PHP script, it must end in a semicolon. You would, of course, have to enclose this in your standard PHP tags, making the following code:
<?php print("My first line of PHP!"); echo("My 2nd line of PHP!"); ?>
Which will display:
My first line of PHP!
My 2nd line of PHP!
on the Your screen.
Note: You might have noticed that our main file is now index.php instead of index.html. This is important! It tells the PHP interpreter that there’s PHP code in the file to evaluate.
PHP allows you to define variables. In PHP there are several variable types, but the most common is called a String. It can hold text and numbers. All strings begin with a $ sign. To assign some text to a string you would use the following code:
$firstvariables = “this is First variables.”;
String is a word or phrase between quotes, like so:
"hi BD, New world!"
This is quite a simple line to understand, everything inside the quotation marks will be assigned to the string. You must remember a few rules about strings though:
Strings are case sensetive so $FirstVariables($First_Variables) is not the same as $firstvariables ($first_variables). String names can contain letters, numbers and underscores but cannot begin with a number or underscore. When assigning numbers to strings you do not need to include the quotes so:
$user_id = 723
Would be Allowed.
PHP Outputting Variables:
To display a variable on the screen uses exactly the same code as to display text but in a slightly different form. The following code would display your welcome text:
<? $firstvariables = "This is First variables."; echo($firstvariables ); ?>
As you can see, the only major difference is that you do not need the quotation marks if you are printing a variable.
<?php echo "Hello," . " " . "world" . "!"; ?>
PHP Formatting Your Text
Unfortunately, the output from your PHP programs is quite boring. Everything is just output in the browser’s default font. It is very easy, though, to format your text using HTML. This is because, as PHP is a server side language, the code is executed before the page is sent to the browser. This means that only the resulting information from the script is sent, so in the example above the browser would just be sent the text:
Hello and welcome to my Code Exercise.
This means, though, that you can include standard HTML markup in your scripts and strings. The only problem with this is that many HTML tags require the ” sign. You may notice that this will clash with the quotation marks used to print your text. This means that you must tell the script which quotes should be used (the ones at the beginning and end of the output) and which ones should be ignored (the ones in the HTML code).
For this example I will change the text to the Arial font in red. The normal code for this would be:
<font face=”verdana” color=”#FF0000″>
As you can see this code contains 4 quotation marks so would confuse the script. Because of this you must add a backslash before each quotation mark to make the PHP script ignore it. The code would change to:
<font face=\”verdana\” color=\”#FF0000\”>
You Can Now Include This In Your Print/echo Statement:
echo (“<font face=\”verdana\” color\”#FF0000\”>Welcome to my CodeExercise Website</font>”);
Browser Display Result:
Welcome to my CodeExercise Website
Because it has only been sent the code:
<font face="Arial" color="#FF0000">Welcome to my CodeExercise Website.</font>
This does make it quite difficult to output HTML code into the Browser but later in this Tutorial I will show you another way of doing this which can make it a bit easier.
Best PHP Tutorial For Beginners Part- 3
PHP Tutorial Part 3 I will introduce IF/Else (something == something else) statements.