aeafisme23 Posted April 15, 2009 Share Posted April 15, 2009 Basically I am having difficulties structuring my code. I want to echo out a variable only if it meets the criteria (a password) if it does not then the average user would never know that it existed. I tried rearranging the code a few times but I am back to square one. End result i get : Your name is and your phone number is: the variables are not assigned because its being called before the variables are set... i understand this, if i put it after the if code then everyone can see it, i just want password people to see it. Any ideas? Thanks so much. <?php $kvcc = "Your name is $name and your phone number is: $phone This is all pertinent information for all that correctly logged in, no one else should be able to see it"; ?> <?php if ($password == "xxx1"){ $name = "Rick Baggot"; $phone = "not available"; echo $kvcc; } elseif ($password == "xxx2"){ $name = "Richard Burnett"; $phone= "not available"; echo $kvcc; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154216-solved-echoing-variables-incorrectly-due-to-bad-structure/ Share on other sites More sharing options...
taquitosensei Posted April 15, 2009 Share Posted April 15, 2009 try this <?php if ($password == "xxx1"){ $name = "Rick Baggot"; $phone = "not available"; } elseif ($password == "xxx2"){ $name = "Richard Burnett"; $phone= "not available"; } echo "Your name is $name and your phone number is: $phone This is all pertinent information for all that correctly logged in, no one else should be able to see it"; Quote Link to comment https://forums.phpfreaks.com/topic/154216-solved-echoing-variables-incorrectly-due-to-bad-structure/#findComment-810750 Share on other sites More sharing options...
jkewlo Posted April 15, 2009 Share Posted April 15, 2009 Try this. Not sure if it will work as I have not tested it. <?php if ($password == "xxx1"){ $name = "Rick Baggot"; $phone = "not available"; echo $kvcc; } elseif ($password == "xxx2"){ $name = "Richard Burnett"; $phone= "not available"; echo $kvcc; } $kvcc = "Your name is $name and your phone number is: $phone This is all pertinent information for all that correctly logged in, no one else should be able to see it"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154216-solved-echoing-variables-incorrectly-due-to-bad-structure/#findComment-810752 Share on other sites More sharing options...
aeafisme23 Posted April 15, 2009 Author Share Posted April 15, 2009 Thanks for the replies, unfortunately the first one is not what I am wanting to do jkewlo you tried exactly what I did but that does not work from what i found. HOWEVER, this did work: <?php if ($password == "ccrab1"){ $name = "Randy Bennett"; $phone = "not available"; $kvcc = "Your name is $name and your phone number is: $phone This is all pertinent information for all that correctly logged in, no one else should be able to see it"; echo $kvcc; } elseif ($password == "ccrab2"){ $name = "Richard Burnett"; $phone= "not available"; $kvcc = "Your name is $name and your phone number is: $phone This is all pertinent information for all that correctly logged in, no one else should be able to see it"; echo $kvcc; } ?> The only thing i do NOT like about this is it's very redundant to have to repost the variable $kvcc numerous times....surely there is a way to get it to work logically like this. I did solve my problem but I am still not happy with having to waste more resources than are necessary...if anyone has an idea I would be much appreciative. Quote Link to comment https://forums.phpfreaks.com/topic/154216-solved-echoing-variables-incorrectly-due-to-bad-structure/#findComment-810759 Share on other sites More sharing options...
MatthewJ Posted April 15, 2009 Share Posted April 15, 2009 why not just do this? <?php $password = "ccrab1"; if ($password == "ccrab1"){ $name = "Randy Bennett"; $phone = "not available"; } elseif ($password == "ccrab2"){ $name = "Richard Burnett"; $phone= "not available"; } echo "Your name is $name and your phone number is: $phone This is all pertinent information for all that correctly logged in, no one else should be able to see it"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154216-solved-echoing-variables-incorrectly-due-to-bad-structure/#findComment-810769 Share on other sites More sharing options...
kenrbnsn Posted April 15, 2009 Share Posted April 15, 2009 Turn it into a function: <?php function echoit($name,$phone) { return("Your name is $name and your phone number is: $phone This is all pertinent information for all that correctly logged in, no one else should be able to see it"); } switch ($password) { case 'ccrab1': $kvcc = echoit('Randy Bennett','not available'); break; case 'ccrab2': $kvcc = echoit('Richard Burnett','not available'); break; } echo $kvcc; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/154216-solved-echoing-variables-incorrectly-due-to-bad-structure/#findComment-810770 Share on other sites More sharing options...
mrMarcus Posted April 15, 2009 Share Posted April 15, 2009 what was wrong with your original post compared to your last post? there is absolutely no need to repeatedly set $kvcc in every statement. as long as the contents of $kvcc are the same, set it (once) and forget it. Quote Link to comment https://forums.phpfreaks.com/topic/154216-solved-echoing-variables-incorrectly-due-to-bad-structure/#findComment-810771 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.