Php
Function array_rand
array_rand => Pick one or more random entries out of an array
This function can be used to fetch one or more elements from an array in php.
Example:
<?php
$a = array(1,2,3,4,5);
$result = array_rand($a);
echo $result;
?>
This will chose a random number from the array $a.
To chose more than one random element from array we ..
Array_multisort function
We can sort multiple arrays based on the values of the first array at once using array_multisort function.
Example:
$a = array(c,a,d,b);
$b = array(5,3,2,7);
To sort the arrays at one use
array_multisort($a, $b);
If you want array be to be sorted in ascending / descending order just use the optional args SORT_DESC, SORT_ASC.
We also have ..
Scandir using php code
we can list files and directories inside a folder or file path using the function scandir
scandir - List files and directories inside the specified path
Returns an array of files and directories from the directory .
$dir = "./temp";
$files = scandir($dir);
print_r($files);
Example output
Array
(
[0] => .
[1] => ..
[2] => filename1
[3] => filename2
)
..
How to get domain name from url
parse_url - Parse a URL and return its components.
If we want the domain name or protocol or arguments from the url, we can just simply use parse url function in php.
Parse_url takes url as argument and returns
scheme - e.g. http
host
port
user
pass
path
query - after the ..
Php constant variable
The PHP constant is very similar to a PHP variable in that it is used to store a value but, unlike a variable, the value cannot be changed.
As the name indicates, the variable will be constant through out the programs life cycle.
We can use the function define to declare and ..
Find error in php file
The below command will scan folder to check syntax error in php files
# find . -name "*.php" -exec php -l {} \;
..
Difference between XML DOM function
The following are the difference between XML DOM functions in PHP 4 and PHP 5.
1)Create new XML document
PHP 4: domxml_new_doc('1.0');
PHP 5: new DomDocument('1.0');
2)create element
PHP 4:$doc->create_element('root');
PHP 5:$doc->createElement('root');
3)Append child
PHP 4:$doc->append_child($ch);
PHP 5:$doc->appendChild($ch);
4)Create text node
PHP 4:$doc->create_text_node($fieldvalue);
PHP 5:$doc->createTextNode($fieldvalue);
5)Set Attribute
PHP 4:$child->set_attribute('attr1', 'attrval1');
PHP 5:$child->setAttribute('attr1', 'attrval1');
6)Get completed XML document
PHP 4:$doc->dump_mem(true);
PHP 5:$doc->saveXML();
..
CAPTCHA Image Code
CAPTCHA - Completely Automated Public Turing test to tell Computers & Human Apart.
How Captcha is used?
Captcha is used as a image recognition tool to get rid of spammers and robot(bot). Only the human can do these image recognition and robots cant recognise these images. So this type of human authentication ..
How to block a code
You can comment single and multiple lines in php in different formats.
<?php
echo "Good Morning";
// This is to comment a single line
# This is a comment a single line
/*
..
Script validation
Cross-Site Scripting
What is Cross Site Scripting?
Cross-Site Scripting is when a visitor is able to input html/javascript code inito a website and have it display this code. Common Causes
When text input is not properly checked for html/javascript tags and is displayed directly to a browser. Examples and their exploits
Example 1 (code):
..
Php imagemagick installation steps
How to install imagemagick on my linux server
You would need imagemagick module in php, when you are working with videos and images. The below steps will give you a brief description on installing imagemagick on your server.
1.) mkdir /home/cpimins
2.) cd /home/cpimins
3.) wget http://layer1.cpanel.net/magick.tar.gz
..
Php script to validate email address
Simple php script to validate email id
The below code is a simple script to validate the email address. The script will check the username, and domain name and the domain list.
function fnValidateEmail($varEmail)
{
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $varEmail))
return(true);
else
return(false);
}
..
Create folder php code
php code to create a directory
Use mkdir() to create a directory in a php. Specify the path where you would like to create a directory or a folder.
Syntax to create a directory:
mkdir(path);
Example:
$path = '/home/w3calc/';
@mkdir($path); //will create a directory in the specified ..
Create and use cookies values
How to use php cookies
Cookies allow the webmaster to store information about the site visitor on their computer to be accessed again the next time they visit. One common use of cookies is to store your username and password on your computer so you don't need to login again each ..
How to check leap year ot not
How to check leap year ot not
The following php function will check whether the given year is a leap year or not.
<?php
function isLeap($yr) {
echo $yr, ': ', (date('L', strtotime("$yr-01-01")) ? 'is Leap Year' : 'is not leap year'), '';
}
?>
The above function will say the argument passed to the isLeap function ..