How to interpret ($newName)?
Does it mean if $newName has been defined or null?
if ($newName)
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: if ($newName)
It means that $newName does not contain an empty / or 0 value.
One of my scripts helped you out? Please donate via Paypal
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: if ($newName)
Got it. Thanks.highend wrote:It means that $newName does not contain an empty / or 0 value.
The "If ($newName)" can't be found in the Bible. Sorry.
-
PeterH
- Posts: 2827
- Joined: 21 Nov 2005 20:39
- Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%
Re: if ($newName)
But please remember: normal variables (but not Global or Perm) return their own name if uninitialized - so if($newname) will be true in this case!
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: if ($newName)
It's a follow-up question from http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=14945
and ofc that variable was initialized before checking for it
and ofc that variable was initialized before checking for it
One of my scripts helped you out? Please donate via Paypal
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: if ($newName)
OK. Got it. Thanks.highend wrote:and ofc that variable was initialized before checking for it
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: if ($newName)
After searching the entire Script Exchange, the "if ($newName)" appears to be Highend's signature XY command. It looks like that Highend is the inventor of "if ($newName)" trick. After experimenting with the "if ($newName)" for a while, nowI realize the following undocumented effects:highend wrote:and ofc that variable was initialized before checking for it
1. the condition will be TRUE if $newName is assigned with any values, eg, "ofc", 123,...etc.
2. the condition will be FALSE if $newName is NOT assigned with any values.
Thank you Highend.
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: if ($newName)
2. and not with 0
I'm not the inventor of a check like that. It's used in all modern computer languages. Normally for boolean values (true / false) but it can be "abused" for content checking as well...
Code: Select all
$a = 0;
if ($a) { echo "true"; }One of my scripts helped you out? Please donate via Paypal
-
PeterH
- Posts: 2827
- Joined: 21 Nov 2005 20:39
- Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%
Re: if ($newName)
...or in short:
0 or "" is false
everything else is true
0 or "" is false
everything else is true
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: if ($newName)
Hi Highend & PeterH,
Thank you for further crystal-clear clarification (CCC).
Yeah, I've been playing with for the last few hours:
$a = 0; if ($a) { echo "true"; }
$a = -0; if ($a) { echo "true"; }
$a = -1; if ($a) { echo "true"; }
$a = a; if ($a) { echo "true"; }
So far PeterH's code is the most compact while Highend is more expansive.
Very different coding styles!!
Thank you for your thorough follow-up.PeterH wrote:...or in short:
0 or "" is false
everything else is true
Thank you for further crystal-clear clarification (CCC).
Yeah, I've been playing with for the last few hours:
$a = 0; if ($a) { echo "true"; }
$a = -0; if ($a) { echo "true"; }
$a = -1; if ($a) { echo "true"; }
$a = a; if ($a) { echo "true"; }
So far PeterH's code is the most compact while Highend is more expansive.
Very different coding styles!!
-
TheQwerty
- Posts: 4373
- Joined: 03 Aug 2007 22:30
Re: if ($newName)
FYI, if performing this test in a place where you cannot be certain that $newName was initialized you should also make use of isset:Generally though you will only need to worry about this when using dereferences, since both global and perm initialize new vars to empty strings.
Code: Select all
if (isset($newName) && $newName) { ... }
XYplorer Beta Club