What's the best way of writing:
if(!$this->uri->segment('4') && $t开发者_如何学Pythonhis->uri->segment('4') != 0)
This is far too long winded. Just need to check if a string is set, even if it's 0.
isset()
EDIT: nope this is not a var its a method..
Just need to check if a string is set, even if it's 0.
if($this->uri->segment('4') != '')
but i dont think this is what you are trying to do.
it depends on what this method returns and what you try to accomplish.
This is far too long winded. Just need to check if a string is set, even if it's 0.
How about checking for the length of the string?
if (strlen($this->uri->segment('4')) > 0)
EDIT For explicitness, I've added > 0, so it may be a little more descriptive what it is exactly you expect. This isn't necessary, however.
The first clause isn't "correct" anyway, as you've discovered by having to write the second one. You also have to consider FALSE
and the empty string (""
). Code like if ($var)
is lazy and, usually, wrong.
The correct approach for testing a variable is the PHP function isset
. However, assuming $this->uri->segment('4')
is a function call, the result will always be "set". It can never not be set. So it seems unlikely to be that you can do much here.
What criteria are you really looking for?
Perhaps your function segment
returns null
? So write if (!is_null($this->uri->segment('4')))
.
Or perhaps you're looking for the empty string? So write if ($this->uri->segment('4') != "")
.
1
if ( strlen($this->uri->segment('4')) )
{}
2
if ( strlen($this->uri->segment('4')) !== "" )
{}
精彩评论