<?php
$array = array('this', 'is', 'an array');
echo is_array($array) ? 'True' : 'False'; // True
echo " ";
$var = 'this is a string';
echo is_array($no) ? 'True' : 'False'; // False
?>
Tag Archives: PHP
Convert strings to an array – PHP
<?php
$testString = "FirstName,MiddleName,LastName";
$testArray = split(",",$testString);
print_r($testArray);
//Array ( [0] => FirstName [1] => MiddleName [2] => LastName )
?>
<?php
$testString = "FirstName MiddleName,LastName";
list($first, $middle, $last) = split('[ ,]', $testString);
echo "First Name: $first; MiddleName: $middle; LastName: $last";
?>
Find out the first occurrence of a string – PHP
<?php $email = 'firstname@mail.com'; $domain = strstr($email, '@'); echo $domain; // prints@mail.com ?>
Function to find out the substring of a string – PHP
<?php
echo substr('abcdef', 1, 3);
// bcd
?>
function to format the price – PHP
<?php $number = 5030; echo number_format($number); // 5,030 ?>
Function to read the character from the ASCII value – PHP
<?php echo chr(50).","; echo chr(55).","; echo chr(53); ?>
Remove spaces from the end of a string – PHP
<?php
echo rtrim("Test "); // Test
//Will remove the spaces from the end
?>
Remove spaces from the beginning of a string – PHP
<?php
echo ltrim(" Test"); // Test
//Will remove the spaces from the beginning
?>
How to find out the number of posts added to a given category – WordPress
<?php
$posts = get_posts(array('category' => 5)); // Here 5 is category id
echo sizeof($posts);
?>
WordPress function to fetch posts from category id
<?php
$posts = get_posts(array('category' => 5)); // Here 5 is category id
?>