PHP Programming

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 –

StartEndTypeNote
<?php?>Standard
<?=?>Echo/Embed
<??>Short formNot recommended. Need to be enabled from the PHP setting (php.ini). Deprecated now.
<script language=”php”></script>Script tagNot recommended.
<%%>ASPDeprecated

Here is the standard way to write PHP code-

Create a PHP file (with extension .php).
Start the PHP part with <?php
End the PHP part with ?>
All our PHP can be between <?php and ?>
<?php

// your PHP code here

?>
PHP

NOTE

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;
PHP

Output:

Hello BigBoxCode
Hello again
Plaintext

We 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" ?>
PHP

NOTE

This is equivalent to-

<?php echo "Hello BigBoxCode"; ?>
PHP

Output:

Hello BigBoxCode
Plaintext

Comments

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
*/
PHP

WARNING

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-

echoprint
assertreturn
exitdie
includeinclude_once
requirerequire_once
listeval
emptyisset
unsetclone
arrayprint_r
newinstanceof
globalstatic
parentconst
namespaceuse
gotofunction
abstractfinal
interfaceyield
foreachfor
whiledo
ifelse
elseifswitch
casedefault
breakcontinue
declare__halt_compiler
trycatch
throwfinally
traitmatch

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)

Standard PHP Library(SPL)

Data Structures

  • SplFixedArray
  • SplObjectStorage
  • SplDoublyLinkedList
    • SplStack
    • SplQueue
  • SplHeap
    • SplMaxHeap
    • SplMinHeap
  • SplPriorityQueue

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.