elseif for new variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

elseif for new variable

Post 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!
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: elseif for new variable

Post by greyhoundcode »

Code: Select all

$xcatid = is_null($xcatid) ? '00' : $xcatid;
Should do the trick.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: elseif for new variable

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: elseif for new variable

Post by pickle »

~greyhoundcode was close. You want this I believe:

Code: Select all

$newstring = is_null($xcatid) ? '00' : $xcatid;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: elseif for new variable

Post by markosjal »

I just tried that as I assumed he was close, but $newstring is not returning 00 when Sxcatid is null
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: elseif for new variable

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: elseif for new variable

Post 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
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: elseif for new variable

Post 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.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: elseif for new variable

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: elseif for new variable

Post by pickle »

Just change your condition to check for a space instead of NULL

Code: Select all

$newstring  = ($xcatid == " ") ? '00' : $xcatid;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: elseif for new variable

Post 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 " ".
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: elseif for new variable

Post by AbraCadaver »

Code: Select all

var_dump($xcatid):
Will tell you exactly what it is.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply