Showing posts with label PHP Function. Show all posts
Showing posts with label PHP Function. Show all posts

Tuesday, 31 December 2019

Check for Palindrome string using PHP


The characters read the same backward as forward. Some examples of palindromic words are redivider, deified, civic, radar, level, rotor, kayak, reviver, racecar, redder, madam, and refer.




function palindrome($string)
{
    return strrev($string) == $string ? true : false;
} 



Results





echo palindrome('SOOS') == true ? 'This is Palindrome' : 'This is not Palindrome';

Output: This is Palindrome
Explanation: Reversing SOOS will also get SOOS





echo palindrome('SOOSS') == true ? 'This is Palindrome' : 'This is not Palindrome';

Output: This is not Palindrome
Explanation: Reversing SOOSS will get SSOOS




Wednesday, 6 August 2014

Playing With Date in PHP

PHP program that prints every date from your birthday to three months after. On the 1st of each month, write the month name. Write “Free lunch” next to fridays and on dates where the day number is multiple of five print “Casual” next to the day {Day Name}.
Sample Section of Output
18 Mar 1986
19 Mar 1986
20 Mar 1986 - Casual Thursday
21 Mar 1986 - Free lunch
22 Mar 1986
23 Mar 1986
24 Mar 1986
25 Mar 1986 - Casual Tuesday
26 Mar 1986
27 Mar 1986
28 Mar 1986 - Free lunch
29 Mar 1986
30 Mar 1986 - Casual Sunday
31 Mar 1986
1 Apr 1986
format("d M Y");

    $get_day = date('d M Y', strtotime($get_day));
    $day_of_month = date('d', strtotime($get_day));
    $toChecksat = date('D', strtotime($get_day));

    if ($day_of_month == '01') {
        $first_date_of_month = new DateTime($get_day);
        echo $first_date_of_month->format('F') . "
"; echo $all = $date->format("d M Y") . "
"; } else { if ($toChecksat == 'Sat') { //Check for SATURDAY echo $all = $date->format("d M Y") . " - Free lunc" . "
"; } else if (($day_of_month % 5) == 0) { //devided by 5 $full_day_name = new DateTime($get_day); echo $all = $date->format("d M Y") . " - Casual " . $full_day_name->format('l') . "
"; } else { echo $all = $date->format("d M Y") . "
"; } } } ?>

Wednesday, 1 January 2014

PHP function Returne correct IP address even if user is behind proxy

 
/**
     * Returne string correct IP address even if user is behind proxy
     * @return string correct IP address even if user is behind proxy
     * @return type
     */
    public static function getIP() {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        }
        elseif (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        }
        else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }