Page 1 of 1
elseif for new variable
Posted: Mon Apr 19, 2010 1:43 pm
by markosjal
I think this is a pretty simple request that I might be able to express well enough
I have an existing variable
$xcatid
From this variable I want to define a new variable"$newstring"
So I need something like (I know this is not like a PHP expression but hopefully inttelligible, think of spreadsheet expressions might help)
if((len[$xcat1d]=0),$newstring="00", $xcatid)
or
if ($xcatid="", $newstring="00", $xcatid)
In otherwords if the $xcatid returns "" or NULL, I need to make the value "00" in the new variable $newstring. If it is a valid value such as "1" , or "100", I need to pass it on to the new varianle.
I am sure this is attainable through only two or three lines of code but I can find no example of elseif that seem to apply
Obviosly I am not much of a PHP guy , but I have a good idea of what I need, just not how to cobnstruct in in PHP
Mark
kludging on!
Re: elseif for new variable
Posted: Mon Apr 19, 2010 2:00 pm
by greyhoundcode
Code: Select all
$xcatid = is_null($xcatid) ? '00' : $xcatid;
Should do the trick.
Re: elseif for new variable
Posted: Mon Apr 19, 2010 3:04 pm
by markosjal
am I mistaken that this changes the string globally?
What I need is a new variable as I can not change orther subsequent occurences. In other word I need that string left in tact an a new one with 00 in place of "" or null
Re: elseif for new variable
Posted: Mon Apr 19, 2010 3:12 pm
by pickle
~greyhoundcode was close. You want this I believe:
Code: Select all
$newstring = is_null($xcatid) ? '00' : $xcatid;
Re: elseif for new variable
Posted: Mon Apr 19, 2010 3:19 pm
by markosjal
I just tried that as I assumed he was close, but $newstring is not returning 00 when Sxcatid is null
Re: elseif for new variable
Posted: Mon Apr 19, 2010 3:22 pm
by pickle
It has to be - that code is correct. Likely what is happening is your Sxcatid variable isn't being evaluated as the null value by PHP. Maybe it's the string "NULL" or the numeric value zero?
Re: elseif for new variable
Posted: Mon Apr 19, 2010 3:42 pm
by markosjal
I am echoing that value on the screen in the corner ant it shows up blank when I suspect it is null.
Furthermore because I am constructing a URL from it I can see that it goes to /buttons//file.txt and in all other instances goes to /buttons/1/file.txt
Maybe I have something wrong is the syntax or a typo . Will check that
Re: elseif for new variable
Posted: Mon Apr 19, 2010 10:29 pm
by markosjal
turns out looks like it is returning a space
how can I modify this from NULL to " " ,<space> any ideas?
And thanks guys for the help.
Re: elseif for new variable
Posted: Mon Apr 19, 2010 10:32 pm
by markosjal
turns out looks like it is returning a space
how can I modify this from NULL to " " ,<space> any ideas? I had to use some dashes in my diagnostic eco to determine it was actually a space and not a NULL.
Maybe something like
$newstring = " "($xcatid) ? '00' : $xcatid;
would that be right?
And thanks guys for the help.
_________
Update, it did not work to change is_null to ' ' but I do not profess to be a php guru
Re: elseif for new variable
Posted: Tue Apr 20, 2010 9:50 am
by pickle
Just change your condition to check for a space instead of NULL
Code: Select all
$newstring = ($xcatid == " ") ? '00' : $xcatid;
Re: elseif for new variable
Posted: Sat May 22, 2010 11:58 pm
by markosjal
Just FYI the code posted here never worked for me. I do not lknow what was being returned however I ended up making the script look for a numeric value. Whatever it was returning it was not NULL, "", nor " ".
Re: elseif for new variable
Posted: Sun May 23, 2010 11:10 am
by AbraCadaver
Will tell you exactly what it is.