Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-18-2008, 08:49 PM   PM User | #1
owt200x
Regular Coder

 
Join Date: Sep 2008
Location: Oklahoma
Posts: 249
Thanks: 11
Thanked 13 Times in 13 Posts
owt200x is an unknown quantity at this point
Question if then help

This may seem stupid, but I can only do basic if then statements.

What I dont know how to code is if variable has data then another variable equals something.

Im using variables in a config.php, and i need code to determine if a variable has any data, then makes a new variable have data, but if teh first variable is empty, then the new variable in empty.

Please help me, OwT

Last edited by owt200x; 10-19-2008 at 10:16 PM..
owt200x is offline   Reply With Quote
Old 10-18-2008, 09:12 PM   PM User | #2
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Code:
if ($a == "1")
{
//Do code
}

elseif ($a == "2")
{
//Do code
}

elseif ($a == "3")
{
//Do code
}

else {
//Do code
}
You can have as many elseif's as you want. But only 1 IF and 1 ELSE for a certain variable.
masterofollies is offline   Reply With Quote
Old 10-18-2008, 10:44 PM   PM User | #3
owt200x
Regular Coder

 
Join Date: Sep 2008
Location: Oklahoma
Posts: 249
Thanks: 11
Thanked 13 Times in 13 Posts
owt200x is an unknown quantity at this point
that would work, but i need it to determine if the variable has a value at all. then do code. I wont know what the variables value will be, so i wouldnt be able to get it to work with that

Last edited by owt200x; 10-18-2008 at 10:46 PM.. Reason: needed to clarify response
owt200x is offline   Reply With Quote
Old 10-19-2008, 02:44 AM   PM User | #4
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Where are the values coming from? Provide us with more information. Your post was pretty vague to begin with.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 10-19-2008, 04:09 AM   PM User | #5
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Quote:
and i need code to determine if a variable has any data, then makes a new variable have data, but if teh first variable is empty, then the new variable in empty.
Check if variable has any value.

Code:
echo "$value";
if variable is empty

Code:
if ($value == "")
{
//Its empty
}
else {
//Its not empty
}

Or you could do this. To change a variable if its empty into one with data.

Code:
if ($value == "")
{
$value = 1;
}
masterofollies is offline   Reply With Quote
Old 10-19-2008, 04:28 AM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by owt200x View Post
that would work, but i need it to determine if the variable has a value at all. then do code. I wont know what the variables value will be, so i wouldnt be able to get it to work with that
the variable exists?
PHP Code:
if(isset($namevar)){
  print 
"is true => exists";
}else{
  print 
"is false => not exists";

is variable empty?
PHP Code:
if(empty($namevar)){
  print 
"yes";
else
  print 
"no"
is variable not empty? (negate something using ! operator)
PHP Code:
if(!empty($namevar)){
  print 
"no"// because not true => false
else
  print 
"yes"
variable exists and is not empty?
PHP Code:
if(isset($namevar) && !empty($namevar)){
  print 
"yes";
}else{
  print 
"no";

the best way to learn something is the manual:
language reference
control structure, if, for, ...
operators, empty, isset, !, &&, ||, and, or, not,....
when you are stuck with something ask, always is somebody who help you to make things clear,

best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
owt200x (10-19-2008)
Old 10-19-2008, 02:38 PM   PM User | #7
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Whats great about PHP is there is a dozen ways of writing the same piece of code using different methods.
masterofollies is offline   Reply With Quote
Old 10-19-2008, 09:51 PM   PM User | #8
owt200x
Regular Coder

 
Join Date: Sep 2008
Location: Oklahoma
Posts: 249
Thanks: 11
Thanked 13 Times in 13 Posts
owt200x is an unknown quantity at this point
Smile

Quote:
Originally Posted by oesxyl View Post
PHP Code:
if(empty($namevar)){
  print 
"yes";
else
  print 
"no"
I appologize my question wasn't very clear, i didn't exactly know how to put what i needed, but this is what i needed. thank you all for helping.

btw, the variables would be included from another php file, for config settings in the main script.


Last edited by owt200x; 10-19-2008 at 09:57 PM.. Reason: added where variables came from
owt200x is offline   Reply With Quote
Reply

Bookmarks

Tags
config, ifthen, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:48 AM.


Advertisement
Log in to turn off these ads.