Jump to content

[SOLVED] Echoing Variables incorrectly due to bad structure


aeafisme23

Recommended Posts

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;
} 	

?>

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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";
?>

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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";    

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.