First PHP Script | First PHP Program
Some important things to know before starting first PHP script :
- A PHP file contains tags of HTML and some code of PHP script.
- A PHP script always starts with <?php and ends with ?>
Example : <?php //your code ?>
Because when PHP parse a file, it looks for opening and closing tags, which tells PHP to start and stop interpreting the code between them.
- You can write PHP script anywhere in your document.
- PHP script should be saved with the file extension .php
- Each code line un PHP must end with a semicolon (;) .
- To display a text mainly “echo” and “print” is used.
- Whitespaces i.e. spaces, newlines and tabs are ignored PHP syntax.
- PHP is case sensitive.
- To make a comment in PHP // or # is used for single line comment and /* */ for multi-line comment.
- To insert special characters use backslash .
Example : echo ”” Hi..How are You? ””; Output : “Hi..How are You?”
There are four different ways of opening and closing tags used in PHP.
- <?php ?>
- <script language=”php” > </script>
- <? ?>
- <% %>
First two options are always available and other two options are short tags and AP style tags that can be turned on and off from php.ini configuration file. Lets us see the first PHP script : Step 1 : Open the text editor (Ex : Notepad) Step 2 : Write the following code snippet
Step 3: Save the file with .php extension. Step 4: Run the file in browser and see the output.
To display a text mainly “echo” and “print” is used but there is one difference between echo and print statement.
- echo allows you to print one or more strings.
- print only allows one string.
Example : <?php echo “Hello”, “World”; echo “<br>”; // print “Hello”, “World”; This will give error print “Welcome to Code.freefeast.info”; ?>