Hướng dẫn dùng string null trong PHP

There is no better way but since it's an operation you usually do quite often, you'd better automatize the process.

Most frameworks offer a way to make arguments parsing an easy task. You can build you own object for that. Quick and dirty example :

class Request
{

    // This is the spirit but you may want to make that cleaner :-]
    function get[$key, $default=null, $from=null]
    {
         if [$from] :
             if [isset[${'_'.$from}[$key]]];
                return sanitize[${'_'.strtoupper[$from]}[$key]]; // didn't test that but it should work
         else
             if isset[$_REQUEST[$key]]
                return sanitize[$_REQUEST[$key]];

         return $default;
    }

    // basics. Enforce it with filters according to your needs
    function sanitize[$data]
    {
          return addslashes[trim[$data]];
    }

    // your rules here
    function isEmptyString[$data]
    {
        return [trim[$data] === "" or $data === null];
    }


    function exists[$key] {}

    function setFlash[$name, $value] {}

    [...]

}

$request = new Request[];
$question= $request->get['question', '', 'post'];
print $request->isEmptyString[$question];

Symfony use that kind of sugar massively.

But you are talking about more than that, with your "// Handle error here ". You are mixing 2 jobs : getting the data and processing it. This is not the same at all.

There are other mechanisms you can use to validate data. Again, frameworks can show you best pratices.

Create objects that represent the data of your form, then attach processses and fall back to it. It sounds far more work that hacking a quick PHP script [and it is the first time], but it's reusable, flexible, and much less error prone since form validation with usual PHP tends to quickly become spaguetti code.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty[] function to check whether a string is empty or not.

    The function is used to check whether the string is empty or not. It will return true if the string is empty.

    Syntax:

    bool empty[string]

    Parameter: Variable to check whether it is empty or not.

    Return Value: If string is empty, it returns true and false otherwise.

    Example 1: PHP program to check whether the string is empty or not.

    PHP

    Example 2:

    PHP

    The special null value represents a variable with no value. null is the only possible value of type null.

    A variable is considered to be null if:

    • it has been assigned the constant null.

    • it has not been set to any value yet.

    • it has been unset[].

    Syntax

    There is only one value of type null, and that is the case-insensitive constant null.

    See also the functions is_null[] and unset[].

    Casting to null

    Warning

    This feature has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this feature is highly discouraged.

    Casting a variable to null using [unset] $var will not remove the variable or unset its value. It will only return a null value.

    quickpick

    11 years ago

    Note: empty array is converted to null by non-strict equal '==' comparison. Use is_null[] or '===' if there is possible of getting empty array.

    $a = array[];

    $a == null  $person['cell'] ];
    }

    print_r[$friends];

    Array
    [
        [0] => Array
            [
                [Name] =>
                [Phone] =>
            ]

        [1] => Array
            [
                [Name] =>
                [Phone] =>
            ]

    ]

    This means that:
    * NULL == NULL['foo']['bar']['whatever']

    This can be slightly confusing if you accidentally slip a NULL into an array of other items.

    Hayley Watson

    4 years ago

    NULL is supposed to indicate the absence of a value, rather than being thought of as a value itself. It's the empty slot, it's the missing information, it's the unanswered question. It's not a jumped-up zero or empty set.

    This is why a variable containing a NULL is considered to be unset: it doesn't have a value. Setting a variable to NULL is telling it to forget its value without providing a replacement value to remember instead. The variable remains so that you can give it a proper value to remember later; this is especially important when the variable is an array element or object property.

    It's a bit of semantic awkwardness to speak of a "null value", but if a variable can exist without having a value, the language and implementation have to have something to represent that situation. Because someone will ask. If only to see if the slot has been filled.

    Anonymous

    4 years ago

    Note: Non Strict Comparison '==' returns bool[true] for

    null == 0

    Chủ Đề