Web Design,Programming,Mobile,iOS/Android,GameDev,IT Businness,eCommerce,Social Media

Advertisement

Tuesday 11 February 2014

PHP Basics - Syntax and tags

18:01


As you noticed in my first tutorial Getting Started with PHP - What is PHP ? we used opening and closing tags to start writting php commands. You will need these tags whenever you want to write php code :

<?php
  //PHP code here
?>
So the very first thing you will do when writting php code make sure you have those tags and your actual code inside. The file where you are writing php code must have the .php extension. If you think you can write these lines in an .html file and run them properly in a browser then you are wrong. PHP files have the .php extension and thus the browser recognizes them and tells the server to execute those lines of code inside the opening and closing php tags.

Basic Printing

In the first tutorial we used to print a string on screen. To do that we used the echo command. This command is used to print and show the result of anything you want to. You can print variables ( more on variables in next tutorial ), strings and other stuff. We will get into more detail in the upcoming tutorials.

Basically what we did to print that string on screen is used the echo command and the string wrapped inside double quotes.

<?php
  echo "This is a string in double quotes to be printed on screen";
?>
Pleace notice the semicolon in the end of the command. Since PHP is written in C and thus it has the syntax rules of C language. So every command that you enter, except keywords, functions, classes etc ( if you are familiar with these thats ok, but if not don't worry just continue we will get into those later on ), must end with a semicolon which means the end of the given command.

PHP can be mixed with html elements to stylize your strings. Take a look at this example

<?php
  echo "<h1>This is a h1 tag inside PHP</h1>";
?>
As you can see you can wrap your strings with actual html tags inside the double quotes in an echo command, and the result will be as we would do in plain html. You may ask yourself why whould i need to put html tags inside php commands when i can just write the html tag.

Keep in mind that PHP can do a lot more than just basic printing, and when you will get into more complex stuff, like calculations data processing and other things, you will need to retrieve those results with php commands and wrapped with html tags.

Go ahead and do some experiments with the echo command and with html tags inside, like styling them with CSS or inline CSS. You will see that the result will be as you would write plain HTML. This is the basic syntax in PHP, in the upcoming tutorials we will talk more about variables.

Written by

Thank you for taking your time to browse through our blog. We try to build as much rich content base as we can. We focus in modern technologies, computer science design and business.

1 comments :

  1. Casino site ᐈ Welcome Bonus up to ₹1500 + 150 Free Spins
    Casino sites like Slots Empire offer an impressive luckyclub collection of slots, table games, and live dealer games, along with progressive jackpots for their loyal

    ReplyDelete

 

© 2013 smartnoob . All rights resevered. Designed by Templateism