Below is the simple function to get class name without namesapce.
1 2 3 4 |
public function getClassName() { $path = explode('\\', __CLASS__); return array_pop($path); } |
Full Stack LAMP, MEAN, DevOps Consultant
Below is the simple function to get class name without namesapce.
1 2 3 4 |
public function getClassName() { $path = explode('\\', __CLASS__); return array_pop($path); } |
Here is simple PHP code snippet to check if a given number is odd or even. Check Number Is Odd Or Even
1 2 3 |
function checkEven($val){ return ($val%2 == 0); } |
1 2 3 4 5 6 |
$val = 7; if(checkEven($val)){ echo "Number is even"; } else { echo "Number is odd"; } |
1 2 3 4 5 6 7 8 9 10 11 12 |
//here we have one php variable <?php $var = "This is my string variable in php"; ?> //In javascript/jQuery <script type = "javascript"> var spge = '<?php echo $var ;?>'; // need to put single/double quote there for string variable alert(spge); </script> |
1 2 3 4 5 |
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { /* This is one ajax call } |
func_get_args() returns an array with all arguments of the current function.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php function sum($a,$b,$c,$d) { $numbers = func_get_args(); $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc; } echo sum(1, 2, 3, 4); ?> |
The array_change_key_case() function changes all keys in an array to lowercase or uppercase. Syntax:
1 2 3 4 |
array_change_key_case(array,case); array - The array to work on CASE_LOWER - Default value. Changes the keys to lowercase CASE_UPPER - Changes the keys to uppercase |
1 2 3 4 5 |
<?php $str="foo,bar,baz,bat"; $arr=explode(",",$str); // print_r($arr); ?> |
Below function takes hex code (e.g. #eeeeee), returns array of RGB values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function hex2rgb( $colour ) { if ( $colour[0] == '#' ) { $colour = substr( $colour, 1 ); } if ( strlen( $colour ) == 6 ) { list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] ); } elseif ( strlen( $colour ) == 3 ) { list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] ); } else { return false; } $r = hexdec( $r ); $g = hexdec( $g ); $b = hexdec( $b ); return array( 'red' => $r, 'green' => $g, 'blue' => $b ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/** * Get google fonts * * @param integer $amount - Return a chuck of fonts * * @return Array of fonts */ function get_google_fonts( $amount = 30 ) { $fontFile = '/fonts/google-web-fonts.txt'; //Total time the file will be cached in seconds, set to a week $cachetime = 86400 * 7; if(file_exists($fontFile) && $cachetime < filemtime($fontFile)) { $content = json_decode(file_get_contents($fontFile)); } else { $googleApi = 'https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key={API_KEY}'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $googleApi); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $fontContent = curl_exec($ch); curl_close($ch); $fp = fopen($fontFile, 'w'); fwrite($fp, $fontContent); fclose($fp); $content = json_decode($fontContent); } if($amount == 'all') { return $content->items; } else { return array_slice($content->items, 0, $amount); } } |
1 2 3 4 5 6 7 8 9 10 11 |
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://devicecloud.digi.com/ws/v1/devices/inventory'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FLASE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FLASE); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, 'username:password'); $data = curl_exec($ch); curl_close($ch); $data = json_decode($data); print_r($data); |