Every PHP script executes on a server. When the server process the script, it sent back the output in the form of plain HTML to the browser
<?php
//Your PHP code
?>
Note: You can write your code in a text editor. There are various text editors for PHP like sublime text, notepad++, brackets, bluefish. I prefer notepad++, you can choose any editor you like. You can also use notepad.
Below is an example that would print the text “Hello World”
<?php
echo "Hello World";
?>
Output
Hello World
PHP Delimiters
<?php and ?> used in this program are the PHP delimiters. The delimiters in a php script can be written in two ways –
1. Canonical tag form – The first is what we have used is called the canonical tag form. In this approach, the PHP script starts with a <?php and ends with ?>.
2 – Short open tag or SGML – There is another form of these delimiters which is called short open tag or SGML and is often used by php developers. In this approach the php script starts with <? and ends with ?>.
Note: To work with the short open tag approach, you have to set the short_open_tag setting in the php.ini file to ‘On’.
As we are in a learning stage, we will be using the canonical tag form in the entire tutorial series.
A PHP script can be embedded anywhere inside an HTML file or a JavaScript file. But the file name must end with .php. This is very important. A PHP file can contain PHP along with HTML, CSS, JavaScript, JQuery and Ajax. I repeat that if PHP involved in any page then that page should have the extension .PHP.
Below is an example containing html and PHP together. If you want to name this file as ‘test’ then you have to save the file as test.php.
<!doctype html>
<html>
<body>
<h1><?php echo "This is a test page";?></h1>
<p>
<?php echo "This is a test paragraph";?>
</p>
</body>
</html>
When this page is opened in a browser, the heading would appear ‘This is a test page’ and a paragraph which would contain the text “This is a test paragraph”.
Note : In PHP, statements ends with a semicolon;
Case Sensitivity
In PHP, keywords (such as echo, while), functions are not case sensitive. That means writing echo and Echo both are legal and would work the same. For example-
<?php
echo "Hello World";
EcHo "Hello World";
ECHO "Hello World";
?>
Note: Variable names in PHP are case sensitive!
For example, $a and $A are not the same but two different variables, we will learn about variables in depth in other chapter.
Predefined keywords in PHP
In a programming language, Keywords are a set of predefined words that are used for specific purposes. These words cannot be used to declare variable names and functions. Here is a table containing predefined keywords in PHP.
abstract | and | array | as |
break | case | catch | class |
clone | const | continue | declare |
default | die | do | echo |
else | elseif | empty | enddeclare |
endfor | endforeach | endif | endswitch |
endwhile | eval | exit | extends |
final | for | foreach | function |
global | if | impliments | include |
include_once | instanceof | interface | isset |
new | or | private | |
protected | public | require | require_once |
return | static | switch | throw |
try | unset | use | while |