Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:50:46+00:00 2026-06-10T07:50:46+00:00

I have the following function to safely get a cookie: public static function get_cookie($parameter,

  • 0

I have the following function to safely get a cookie:

public static function get_cookie($parameter, $default)
{
    return isset($_COOKIE[$parameter]) ? $_COOKIE[$parameter] : $default;
}

When I try to read false then use it in ternary operator, I see the value is treated as string (which is casted to true).

I want to pass a type into this function and cast the value, but have no ideas how.

UPDATE

As Niko pointed, casting ‘false’ to boolean doesn’t work: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

I guess, I have to store strings in cookies always. (For instance, ‘yes’ and ‘no’ instead of ‘false’ and ‘true’ for my case).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:50:48+00:00Added an answer on June 10, 2026 at 7:50 am

    There is absolutely no need to do the casting inside the function (especially since PHP is loosely typed).

    Consider the following use-case:

    $booleanValue = ClassName::get_cookie('foo', true, 'bool');
    

    You end up with the same amount of code (but much more readable!) when you do the casting outside of get_cookie():

    $booleanValue = (bool) ClassName::get_cookie('foo', true);
    

    However, you can still implement a simple switch for ‘false’ and ‘true’ strings, respectively:

    public static function get_cookie($parameter, $default, $isPseudoBool = false) {
        $value = isset($_COOKIE[$parameter]) ? $_COOKIE[$parameter] : $default;
    
        if ($isPseudoBool) {
            $value = ($value === true || strcasecmp($value, 'true') === 0);
        }
    
        return $value;
    }
    

    If you still prefer to move the type conversion into the function, settype() is what you need for this:

    public static function get_cookie($parameter, $default, $type) {
        $value = isset($_COOKIE[$parameter]) ? $_COOKIE[$parameter] : $default;
        settype($value, $type);
        return $value;
    }
    

    But please note that this won’t convert a string “false” to the boolean value false, if you specify $type = 'bool' – the conversion rules are the same as when an implicit conversion is done by the interpreter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following function in one Activity public void AppExit() { Editor edit =
have the following function on my collection : getFiltered: function (status, city) { return
i have following function: function fnGetIdentifiers(id){ $.get( 'getCultureIdentifiers.php?cultureID=' + id, function(data){ if (data.length >
I have following function into to get the text between tags but it is
I have following function in C++ int readData ( Class1 *data) { //StartTime try{
I have the following function, it will always return True. Any ideas why and
I have following function in my code public List<string> GetpathsById(List<long> id) { List<string> paths
Suppose we have following function in PHP: public function testSomething() { $name = perform_sql_query("SELECT
I have the following function: public DataTable GetRecordSet(String query, string[] parameters) It is typically
Let's say I have following function: int foo (int a) { return something; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.