DragonPrime - LoGD Resource Community
Welcome Guest
  • Good morning, Guest.
    Please log in, or register.
  • October 23, 2017, 04:25:19 AM
Home Forums News Downloads Login Register Advanced Search
* * *
DragonPrime Menu
Login
 
 
Resource Pages
Search

Pages: [1]   Go Down
  Print  
Author Topic: What's the difference between 0, "", !, and the end of the world as we know it?  (Read 959 times)
0 Members and 1 Guest are viewing this topic.
DaveS
Mod God
*****
Offline Offline

Posts: 1544


Dfly... you know, as in DaveFly


View Profile
« on: December 28, 2006, 01:28:32 PM »

So sometimes I run into a little problem with this concept of "what is nothing"...

Let's say I have a module preference:

Code:
"shoetie"=>"How many times have you tied your shoe today?,int|",

Then, let's say I want to make sure that people who haven't tied their shoes are punished drastically:

if (get_module_pref("shoetie")==""){
  output("You trip over your shoelaces and die.");
  $session['user']['hitpoints']=0;
  addnav("To the Shades",shades.php);
}

So here's the question... what is the difference between:

1.  if get_module_pref("shoetie")==""
2.  if get_module_pref("shoetie")==0
3.  if !get_module_pref("shoetie")

And what if, for the newday, I  check to see if they tied their shoe the previous day... and if they did I reset the shoetie pref.  What should I use? Which matches up with which?

1.  set_module_pref("shoetie","");
2.  set_module_pref("shoetie",0);
3.  clear_module_pref("shoetie");

Here's what I THINK...

Clear is the same as ""... but 0 is very different from them.  Any clarifications?
Logged

Check out my Last Module:
The Dragon Eggs Expansion
http://dragonprime.net/index.php?topic=8474.0

About my Retirement:
http://dragonprime.net/index.php?topic=9080

See my complete module list here:
http://dragonprime.net/index.php?topic=3038.0
Nightborn
Guest
« Reply #1 on: December 28, 2006, 01:37:18 PM »

the thing is, in PHP you normally don't use a defined return type for a function...

so the function "get_module_pref" just returns... something... whatever it is.

array, int, string, char, whatever.

and so are the functions in PHP ...

Code:
"" == 0

in this case.

if you expect a number to be returned, i.e. if you have a pref where you save a number, then use (int) to cast it.

Code:
$number= (int) get_module_pref("number");

to make sure you get a number and can compare
Code:
if ($number==0) {
//do here what happens when it is 0 or not set.
}

it is... well, freedom in a way, but also tricky.

you don't even need to declare your variables in PHP... once you use it, it is there... and has no type.

//no responsibility, this is how far I understood it
Logged
XChrisX
Global Moderator
Mod God
*****
Offline Offline

Posts: 4647

Be aware of the squirrel!


View Profile WWW
« Reply #2 on: December 29, 2006, 12:55:31 AM »

So here's the question... what is the difference between:

1.  if get_module_pref("shoetie")==""
2.  if get_module_pref("shoetie")==0
3.  if !get_module_pref("shoetie")

The better question to be asked would be using the trinity operator:

Code:
1. if (get_module_pref("shoetie") === "")
2. if (get_module_pref("shoetie") === 0)
3. if (get_module_pref("shoetie") === false)

Here you can see the difference: "===" does check if the left argument and the right argument are exactly the same.

get_module_pref() returns a string as its result. We'll have it return "" in this case.

No. 1 will result in its expression being valid --> "" === "".
No. 2 will result in its expression being invalid.
No. 3 as well.

Why? Because 0 is an integer, not a string. And false is a boolean, not a string.

"==" tries to convert the right argument to the type if the left one and then do a strict comparison.

Code:
if (0 == get_module_pref("shoetie"))
is therefor equal to
Code:
if (0 === (int)get_module_pref("shoetie"))

For basic coding you can simply use "==" for comparison. But in some case you simply need to know, if certain variables match in value AND type.

(Look at the function get_module_pref() ... Wink There's an example on how it is used Tongue )
Logged

Running for more than three years now:
Pages: [1]   Go Up
  Print  
 
Jump to:  


*
DragonPrime Notices
Version 1.1.2 is the current supported version and is available for download.

Support Us
No funds raised yet this year
Your help is greatly appreciated!
Recent Topics
DragonPrime LoGD
Who's Online
28 Guests, 0 Users
Home Forums News Downloads Login Register Advanced Search