PHP is one of the most popular programming languages for web application development. PHP is easy to learn and easy to use.
PHP stands for “PHP Hypertext Preprocessor“
To create and run a PHP script, create a file with any name that has the extension “.php“. Then in the file start the PHP code part with the tag <?php and end with ?> .
PHP Scripting
Here are all the ways we can add or embed PHP code –
Start | End | Type | Note |
---|---|---|---|
<?php | ?> | Standard | |
<?= | ?> | Echo/Embed | |
<? | ?> | Short form | Not recommended. Need to be enabled from the PHP setting (php.ini). Deprecated now. |
<script language=”php”> | </script> | Script tag | Not recommended. |
<% | %> | ASP | Deprecated |
Here is the standard way to write PHP code-
<?php
// your PHP code here
?>
PHPNOTE
There can be multiple occurrences of the PHP tags <?php and ?>.
If there is no other code after the closing tag(?>), then we can ignore the closing tag(?>).
Print Text
We can use the “echo” or “print” construct to output text in PHP. Check the example below-
<?php
echo "Hello BigBoxCode" . PHP_EOL;
print "Hello again" . PHP_EOL;
PHPOutput:
Hello BigBoxCode
Hello again
PlaintextWe can use the short form of the PHP tag to embed some output text in the PHP script.
This tag starts with <?= and ends with ?>. Whatever evaluates in these tags will be printed. This is handy when we want to output/print something in a long HTML code.
<?= "Hello BigBoxCode" ?>
PHPNOTE
This is equivalent to-
<?php echo "Hello BigBoxCode"; ?>
PHPOutput:
Hello BigBoxCode
PlaintextComments
There are 3 ways to add comments in PHP script. We have to star the line with-
- // – start the line with double slash
- # – start the line with hash/pound sign
- /* multi-line comment */ – start with “/*” and end with “*/“. This can be a multi-line comment.
<?php
// This is a single line comment.
// This is the mostly used commenting style in PHP
# This is also a single line comment
/*
This is multi line comment.
whenever we need comment with
multiple lines with use this style
*/
PHPWARNING
We can not use “*/” inside the multi-line comment. If we use “*/” inside the multi-line comment, then it ends the comment at that point.
Language Constructs
We have already used “echo” for outputting texts and echo looks like a function in it’s usage.
NOTE
“echo” is not a function. It is a language construct.
Language constructs are not functions. These are functionality embedded into the PHP language, and implemented as a part of the PHP programming language itself.
NOTE
PHP parser understands the constructs, and these constructs do not need any further parsing, as it is already backed into the language itself.
But normal function need parsing and breaking down into some language construct, to be processed properly.
Here is the list of PHP language constructs-
echo | |
assert | return |
exit | die |
include | include_once |
require | require_once |
list | eval |
empty | isset |
unset | clone |
array | print_r |
new | instanceof |
global | static |
parent | const |
namespace | use |
goto | function |
abstract | final |
interface | yield |
foreach | for |
while | do |
if | else |
elseif | switch |
case | default |
break | continue |
declare | __halt_compiler |
try | catch |
throw | finally |
trait | match |
Here are all the tutorials for understanding PHP programming-
Fundamentals
- Variable
- Data Types
- Constant
- Operator
- Array
Control Statement
- if…else
- switch
- match
Loop
- for Loop
- foreach loop
- while Loop
- do-while Loop
Function
- PHP function
- Arrow Function
- Anonymous Function
- Closure
Object-Oriented Programming(OOP)
- Class and Object
- Interfaces
- Abstract Class and Method
- Traits
Standard PHP Library(SPL)
Data Structures
- SplFixedArray
- SplObjectStorage
- SplDoublyLinkedList
- SplStack
- SplQueue
- SplHeap
- SplMaxHeap
- SplMinHeap
- SplPriorityQueue