PHP Basics
PHP Basics tutorial presents the core Syntax of PHP and its use in connection with how we can embed PHP Code in HTML. The term PHP block is also discussed.
First and most important point is that the PHP is written in the PHP file on top of the HTML syntax. The PHP in the HTML syntax is just like a block of PHP embedded in the HTML syntax.
The PHP block in the HTML is start with the PHP opening tag and end with the PHP closing tag. The PHP opening tag is <?php and PHP closing tag is ?>.
The PHP file must be stored with .php extension, so that the server knows that this files need to be preprocessed with PHP engine.
Basic Syntax of PHP
Inside the PHP block we can write the PHP code. The most common PHP code is the PHP echo function. The PHP echo function is used to print some string or variable value in the HTML.
Caution The PHP file must have file extension .php, otherwise the PHP server will not preprocess the php script.
Example: How to Embed PHP in HTML syntax
<!DOCTYPE html> <html> <body> <h1>PHP Code in HTML</h1> <p>This example shows, how you can embed a PHP block in HTML document.</p> <?php echo "String Output from PHP Block."; ?> <p>You can use normal HTML outside the PHP Block.</p> </body> </html>
The above example shows, how you can embed a PHP block in HTML document. In this code you can see that outside of PHP block you can use normal HTML.
The output of this php file is a simple HTML, because the PHP web server process the PHP file and remove all PHP block with its printable output.
Note: In one PHP file you can use one or more PHP blocks.
Note: You have to use XAMPP software stack to make your PC as PHP web server.
The XAMPP is a software bundle or stack, which included Apache web server and MySQL database. You have to install XAMPP to use your PC as local web server.
How to Embed PHP Code in the HTML with other tag
In the previous example the echo text string is does not have any HTML tag markup. You can specify the HTML tag within echo function as shown in the below example.
Example: PHP output with HTML tag markup.
<!DOCTYPE html> <html> <body> <h1>Output with HTML tags.</h1> <p>This code shows, how you can show string with HTML h3 tag markup.</p> <?php echo "<h3>String Output from PHP Block.</h3>"; ?> <p>You can see the output string now with h3 markup.</p> </body> </html>
The code above shows, how you can use PHP echo function to output HTML tag also. In this code you can see that, now string is markup with h3 tag.
Syntax of PHP with Multiple PHP blocks
In the previous example we have seen that we can output HTML code with the help of echo function simply.
It is really difficult to output big HTML code with the help of PHP echo function. And if you want to output big HTML code you can end the PHP block and then write HTML code and when you want you can start another PHP block.
Example: PHP Code with Multiple PHP block.
<!DOCTYPE html> <html> <body> <h1>PHP syntax with Multiple PHP blocks.</h1> <p>This code shows, how you can use multiple HTML h3 tag markup.</p> <?php echo "<h3>String Output from first PHP Block.</h3>"; ?> <p>Output string in the HTML mode.</p> <?php echo "<h3>String Output from second PHP Block.</h3>"; ?> </body> </html>
The code above shows, how you can use multiple PHP blocks in a PHP file. It is also called switching between PHP mode and HTML mode.
PHP with Variables and Arithmetic Operations
In this example we will see how we can create variables in PHP and perform arithmetic operations.
Example: PHP Code with Variables and Operators.
<!DOCTYPE html> <html> <body> <h1>PHP syntax with variables.</h1> <p>This code shows, how you can use variables in PHP.</p> <?php $num1=10; $num2=20; $num3=$num1+$num2; echo $num3; ?> </body> </html>
In The above code example, we have used three variables and performed addition operation. And finally we have printed the result of addition operation with the help of echo function.
Note: In the PHP language all the variable names must start with dollor ($) symbol.
You have made some good points there. I looked on the net to find out more about the issue and found most people will go along with your views on this website.|