elseif for new variable
Moderator: General Moderators
elseif for new variable
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!
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!
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: elseif for new variable
Code: Select all
$xcatid = is_null($xcatid) ? '00' : $xcatid;Re: elseif for new variable
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
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
~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.
Re: elseif for new variable
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
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.
Re: elseif for new variable
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
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
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.
how can I modify this from NULL to " " ,<space> any ideas?
And thanks guys for the help.
Re: elseif for new variable
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
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
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.
Re: elseif for new variable
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 " ".
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: elseif for new variable
Code: Select all
var_dump($xcatid):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.