PHP Language Basics
- When a PHP file only contains PHP code then we do not need closing tag of PHP ?>
- PHP prints white spaces (you can see in the Page Source), but HTML ignores/removes that white space.
General
- Variable declaration require to start variable with $ sign followed by a character.
- We cannot use - + % for the variable name. However can use underscores. i.e. $first_name
- Variables are case sensative.
- There is no need to initialize the variable as it all happens at the declaration time. i.e. $first_name = "Jhon"
- A variable can hold any type of data i.e. string to integer and vice versa.
- Superglobals are built-in/predefined variables, always available in all of the scopes. (See documentation of Superglobals And Predefined Variables)
Variables
- Constant is the value that does not change.
- We cannot use isset() for constant. This is only to check if a variable is set/defined or not.
- Once constant is defined, the original value cannot be changed even if we reassign another value after defining it.
- Constant scope is global within a page/program.
Constants
- SCALAR Data Type: These are the data types that hold single value.
- There are four SCALAR data types. Boolean, Integer, Float and String.
- SCALAR means it holds single value, unlike arrays, hash table, link table and class
Data Types
-
Not everything is case sensative. We can write echo/Echo/ECHO, print/Print/PRINT and <?PHP but variables are case sensatives.
- The function names are also not case sensative. Greetings(); GREETINGS(); both will work regardless how it was defined.
-
However we cannot define two functions with same name but different cases. i.e.
function Greetings() and function GREETINGS() will NOT work and we will get error.
Case Sensitivity
- include() does not stop the execution of the rest of the page if file is not found which is included.
- require() stops the execution of the rest of the page if file is not found which is required.
- include_once() and require_once() only include the files once even if we try to include same file multiple times.
Include and Require
With the following example the menu.php will only be added once even it is being included twice. include_once(“menu.php ”)
However in the following example it will be added twice.
include_once(“menu.php ”) include(“menu.php ”)