- Unicode
- Alternative PHP Cache
- Namespaces
- late static binding
- mysqlnd
- breaking to a label
- foreach on multidimensional arrays
- improvements to []
Category Archives: PHP
Namespaces
Namespaces is a method with which we can group variable,function or objects so that we can have more than one variable or function or object with same name .
Namespaces were introduced into PHP from version 5.3 onwards
Use
• Helps to prevent accidentally re-defining functions, classes, constants, …
• Avoids having to use long, highly descriptive class names
• Constants, classes, traits, interfaces and functions are affeced by the use of namespaces
• Create sub-namespaces to sub-divide a library
Declaring Namespaces
• Must declare the use of namespaces with the keyword “namespace” at the beginning of the code file (right after <?PHP)
• Use one namespace per code file (best practice)
• Unless a namespace is defined, constants, classes, functions, traits and interfaces are defined with the global namespace
• Within a namespace qualifying with a “\” references the global namespace
• Once code elements within a single namespace are defined, they can be used in other php files
Example
<?php namespace php; class php { public function phpcodez() { echo 'Function1 <br />'; } } namespace codez; class codez { public function phpcodez() { echo 'Function2 <br />'; } } $phpcodez = new phpphp(); $phpcodez->phpcodez(); $phpcodez = new codezcodez(); $phpcodez->phpcodez(); ?>
Check magic quotes off – PHP
<?php echo get_magic_quotes_gpc()?”Enabled”:”Disabled”; ?>
Function
A function is a group of statements that will do certain tasks.
Why Function
When we develop a module in a project , we may need to implement certain tasks(block of statements ) more than once . Writing same block of codes more than once is not at all a good practice . Instead we can give a name to that block and can use them whenever necessary . When we define a block with a name , it is known as function .
• Blocks of code that execute in isolation (and local scope) that perform an action
• Function names are case-insensitive; defined in global scope
• Can be referenced before being defined unless function conditional
• Types: built-in (php supplied); user-defined; externally provided
General Format
function funation_name(){
echo “Error”; // can be on or more lines of statements
}
function – a keyword
funation_name – Any desired name(Should be meaningful)
Once the function is desfined , You can can invoke it by calling its name
<?php funation_name(); ?> // It will print the text “ Error”
A function can have arguments . When a fuction is define using arguments in it , we should pass the values when calling the it .
Example :
function function_sum($a,$b){
echo $a+$b;
}
<?php function_sum(7,8); ?> // It will display 15 as the result .
Note : We should not use any Builtin function name as the function name
PHP functions
A function is a group of statements that can be executed any time you want.
PHP has a number of predefine functions for you to use.
Below given are the different type of funtions available in PHP
- Date functions
- Math functions
- Array functions
- Calendar functions
- Directory functions
- Error functions
- Filesystem functions
- Filter functions
- FTP functions
- HTTP functions
- LibXML functions
- Zip functions
- Mail functions
- Misc functions
- MySQL functions
- SimpleXML functions
- String functions
- XML Parser functions
register_globals
It can be considered as a flag that controls how you access form, server, and environment variables. By default this variable is set to Off, requiring you to use special arrays to access these variables. Its values are set in php.ini file .When the value is set to “on” , PHP will dynamically create global variable for many server variable as well as the query string parameters .
Its always better to set the value as “off”
Magic quotes
Magic quotes is a controversial feature of the PHP scripting language, which was introduced to help newcomers write functioning SQL commands without requiring manual escaping. It was later described and widely misinterpreted as help to prevent inexperienced developers from writing code which is vulnerable to SQL injection attacks. This feature is officially deprecated as of PHP 5.3.0, and removed in PHP 5.4 due to security concerns
Safe mode
Safe mode is a diagnostic mode of a computer operating system (OS). It can also refer to a mode of operation by application software. Safe mode is intended to fix most, if not all problems within an operating system. It is also widely used for removing Rogue security software.
The PHP safe mode is an attempt to solve the shared-server security problem. It is architecturally incorrect to try to solve this problem at the PHP level, but since the alternatives at the web server and OS levels aren’t very realistic, many people, especially ISP’s, use safe mode for now.
- Limit the execution of shell commands
- Limit access to system environment variables
- Limit the paths from which PHP can include files using include or require
Features removed in PHP6
- safe_mode
- magic_quotes
- register_long_arrays
- register_globals
- ereg removed from the core
- long variables (i.e. $HTTP_*_VARS)
- short tag <?
Simple XML
Simple XML is a variation of XML containing only elements. All attributes are converted into elements. Not having attributes or other xml elements such as the XML declaration / DTDs allows the use of simple and fast parsers. This format is also compatible with mainstream XML parsers.