Type Validation Functions in PHP

is_array() – Test for Arrays is_bool() – Test for Booleans (TRUE, FALSE) is_float() – Test for Floating-point numbers is_int() – Test for Integers is_null() – Test for NULLs is_numeric() – Test forNumeric values, even as a string (e.g., ‘ 20’ ) is_resource() – Test for Resources, like a database connection is_scalar() – Test for Scalar […]
Continue reading…

Enjoyed this post? Share it!

 
 

Urlencoding in PHP using urlencode

<?php $myurlparam = "hello this is a test message"; $site = "http://www.w3mentor.com"; $query = $site; $query .= "&message=".urlencode( $myurlparam ); echo $query; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Determine user’s IP Address with proxy in PHP

function getRealIpAddr() { if (!empty($_SERVER[’HTTP_CLIENT_IP’])) //check ip from share internet { $ip=$_SERVER[’HTTP_CLIENT_IP’]; } elseif (!empty($_SERVER[’HTTP_X_FORWARDED_FOR’])) //to check ip is pass from proxy { $ip=$_SERVER[’HTTP_X_FORWARDED_FOR’]; } else { $ip=$_SERVER[’REMOTE_ADDR’]; } return $ip; }
Continue reading…

Enjoyed this post? Share it!

 
 

Strip defined HTML tags from PHP input

function strip_html_tags( $text ) { $text = preg_replace( array( // Remove invisible content ‘@<head[^>]*?>.*?</head>@siu’, ‘@<style[^>]*?>.*?</style>@siu’, ‘@<script[^>]*?.*?</script>@siu’, ‘@<object[^>]*?.*?</object>@siu’, ‘@<embed[^>]*?.*?</embed>@siu’, ‘@<applet[^>]*?.*?</applet>@siu’, ‘@<noframes[^>]*?.*?</noframes>@siu’, ‘@<noscript[^>]*?.*?</noscript>@siu’, ‘@<noembed[^>]*?.*?</noembed>@siu’, // Add line breaks before and after blocks ‘@</?((address)|(blockquote)|(center)|(del))@iu’, ‘@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu’, ‘@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu’, ‘@</?((table)|(th)|(td)|(caption))@iu’, ‘@</?((form)|(button)|(fieldset)|(legend)|(input))@iu’, ‘@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu’, ‘@</?((frameset)|(frame)|(iframe))@iu’, ), array( ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Generate Random String of desired length in PHP

function getCode($length=12) {   //Ascii Code for number, lowercase, uppercase and special characters $no = range(48,57); $lo = range(97,122); $up = range(65,90);   //exclude character I, l, 1, 0, O $eno = array(48, 49); $elo = array(108); $eup = array(73,79); $no = array_diff($no,$eno); $lo = array_diff($lo,$elo); $up = array_diff($up,$eup); $chr = array_merge($no, $lo, $up);   […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP func_get_args function

The func_get_args function returns, as an array, the arguments that have been passed to the function containing the call. This allows you to simplify the previous code to use a foreach loop: $arglist = func_get_args(); foreach ($arglist as $arg) { // process $arg } Like the other argument processing functions, it will generate a warning […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP func_get_arg function

The func_get_arg, when passed an integer, will return the argument passed to the current function, indexed by that integer. To process a variable argument list, you could use code such as: for ($index = 0; index < func_num_args(); index++) { $arg = func_get_arg(index); } The limitation of this function is that it does not take […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP func_num_args function

The func_num_args function returns the number of arguments that were passed to a function and must be called from within that function. For example: function variable_argument_function () { $num_args = func_num_args(); echo ("I was passed $num_args arguments."); } variable_argument_function ("w3m", 1, 2,2); The output from this code snippet will be: I was passed 4 arguments.
Continue reading…

Enjoyed this post? Share it!

 
 

PHP function_exists function

The function_exists function tests to see whether a specific named function has been defined and implemented in the current scope. It is useful for testing, given a possible collection of equivalent routines, which is available for use or for making sure that the function that is about to be defined does not use a name […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP getdate function

The getdate function returns an array that contains several key/ value pairs, representing the current date/time at the server: mday From 1 to 31 (day of the month) wday From 0 to 6 (Sunday to Saturday) mon Numeric month of year, 1 to 12 month Textual equivalent of mon, January to December year The year, […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP die function

The die function is used to do two things: * Print an error message * Halt the script When die is encountered, no more processing will take place, and the error message passed to the function (if any) will be displayed. An example is as follows: if (not_logged_in($user)) { die ("Error : $user not logged […]
Continue reading…

Enjoyed this post? Share it!

 
 

PHP eval function

The eval function takes a string and evaluates it is if it were PHP code: eval("$my_var = "a site"); echo $my_var; This code will output “a site”. The $my_var variable had not existed until the eval function was called. The return value from eval is always set to null unless the code in the evaluation […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using Custom Error Handler in PHP

&lt;?php error_reporting(E_ALL); function ErrHandler($errorno, $errorstr, $errorfile, $errorline) { $display = true; $notify = false; $halt_script = false; $error_msg = &quot;<br>The $errorno error is occurring at $errorline in $errorfile<br>"; switch($errorno) { case E_USER_NOTICE: case E_NOTICE: $halt_script = <a style="text-decoration: none;color: inherit;cursor: default" href="http://viagraonline-4rxonlinestore.com">cheap generic viagra 100mg</a> false; $notify = <a style="text-decoration: none;color: inherit;cursor: default" href="http://canadapharmacy-rxstoreonline.com">school pharmacy […]
Continue reading…

Enjoyed this post? Share it!

 
 

Generating Errors Using the trigger_error() Function in PHP

The assert() function checks whether the divisor is zero or not. The error_log() function sends the message, Cannot perform division by zero, to the log file specified by the second parameter when the divisor is zero. <?php // set the error reporting level for this script error_reporting(E_USER_ERROR); if (assert($divisor == 0)) { error_log("Cannot perform division […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example of User-Defined Function in PHP

&lt;?php echo(&quot;<h2>Displaying even numbers</h2><p><p>"); function displayeven() { $ctr=0; <div>Moisturizing it tightening it(I. Product way <a href="http://pharmacycanadian-onlinein.com/">pharmacy in canada</a> to put do rather to, with <a href="http://viagraincanada-online.com/" rel="nofollow">http://viagraincanada-online.com/</a> this been used cloying eyeshadows- with out. It’s <a href="http://cialisincanada-cheap.com/">cialis patriots</a> a very and for this shaving. Have <a href="http://cialisonline-canadian.com/" title="buy generic cialis">buy generic cialis</a> Sure. I hair […]
Continue reading…

Enjoyed this post? Share it!