Menus

Monday 14 August 2017

PHP more useful functions




PHP strlen() Function

The strlen() function returns the length of a string.

Syntax

strlen(string)

Example

<?php
echo strlen("Hello world!");
?>

PHP substr() Function


Return part of a string


The substr() function returns a part of a string.

Syntax

substr(string,start,length)



ParameterDescription
stringRequired. Specifies the string to return a part of
startRequired. Specifies where to start in the string
  • A positive number - Start at a specified position in the string
  • A negative number - Start at a specified position from the end of the string
  • 0 - Start at the first character in string
lengthOptional. Specifies the length of the returned string. Default is to the end of the string.
  • A positive number - The length to be returned from the start parameter
  • Negative number - The length to be returned from the end of the string

Get first n characters of a string

Examples

echo substr("Hello world",0,4);
Output is print  "Hello"


$category_name=substr($cat['description'],3,  strlen($cat['description'])-1);


PHP fmod() Function


The fmod() function returns the remainder (modulo) of x/y.

Syntax

fmod(x,y);

             if(fmod($i, 2)==0){                  

                  //  Reminder is zero.
             
            }
            else{

                //  Reminder is one.

            }



Converting separate month, day and year values into a timestamp



using setDate funtion

<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>




No comments:

Post a Comment