In PHP, what purpose does (unset) have?

No, I'm not referring to the unset() language construct, but the (unset) type caster. From the PHP manual:

The casts allowed are:

  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
  • (unset) - cast to NULL (PHP 5)

URL: http://php.net/manual/en/language.types.type-juggling.php

Does anyone have any idea about this (or even used it in an actual project)?

Answers


I didn't even know this was a thing, but it seems like the purpose is just completeness for available php primitives (NULL being one of them). Note that this casts the data .. it does not do a write.

$x = 'foon';
$y = (unset)$x;
var_dump($x, $y) // 'foon', NULL

Note that x is not null in spite of the cast.

Near as I can tell, there's no reason to ever use (unset)<anything> as opposed to just writing NULL. Perhaps someone else can come up with a better answer, though.


The only thing I can think of is some future use-case where a class can define how cast operators work, e.g as with per __toString(). A class could potentially be marked non-nullable and hence casting to null would throw an exception. That would be fundamentally different from assigning NULL, although why on earth it's named unset is a mystery.


I use (unset) casting to avoid creating ugly if else statements in situation if you need to validate and use many variables, but if one variable is considered incorrect and you do not want to check remaining.

For example, you have following code:

$variableone = "ffo";
$variabletwo = "obb";
$variablethree = "aar";

if(checkonefailed($variableone))
{
    outputsomething();
}
else
{
    dosomething($variableone)
    if(checktwofailed($variabletwo))
    {
        outputsomething();
    }
    else
    {
        dosomething($variabletwo)
        if(checkthreefailed($variablethree))
        {
            outputsomething();
        }
        else
        {
            dosomething($variablethree)
            //And so own
        }
    }
}

You can rewrite it like this:

$variableone = "ffo";
$variabletwo = "obb";
$variablethree = "aar";

if(checkonefailed($variableone))
{
    outputsomething();
}
//False or check
elseif((unset)(dosomething($variableone))||(checktwofailed($variabletwo)))
{
    outputsomething();
}
elseif((unset)(dosomething($variabletwo))||(checkthreefailed($variablethree)))
{
    outputsomething();
}
elseif((unset)(dosomething($variablethree))/*or next variable*/)
{
  //And so own
}

Idea taken from Go programming code

if variableone := "ffo"; checkonefailed(variableone) {
    outputsomething()
} else if dosomething(variableone); variabletwo := "obb"; checktwofailed(variabletwo) {
    outputsomething()
} else if dosomething(variabletwo); variablethree := "aar"; checkthreefailed(variablethree) {
    outputsomething()
} else dosomething(variablethree)

The purpose is to cast to NULL, like you already wrote in your question:

(unset) - cast to NULL (PHP 5)

So let's say you have a variable $var of which isset($var) is TRUE. You can then cast that variable to NULL:

/**
 * @param mixed $var anything
 */
function func($var)
{
    if (isset($var))
    {
        $var = (unset) $var;
    }
    # the rest of the code expects $var to be NULL
    ...
}

Sure,$var = NULL would do the same here, but w/o casting.


Need Your Help

iPhone UIButton provide different inset values for highlighted state

iphone uibutton textlabel

I want my titleLabel text move a little down on button tap (i.e. I want to give different inset values for highlighted and default states).

How to code ViewModel with Key for human-readable values instead of Ids

sql asp.net-mvc entity-framework

I am getting an error when I perform an Update-Database in PM Console. I understand why, but given my needs I'm not sure how to best overcome it. I hope somebody can point me in the right direction...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.