0

I have something like this:

$msg    = '';
$var1   = 'image';
$var2   = 'class';
$var3   = 'This '.$var1.' has this class assigned:'.$var2;

if($E == 0) {
    $msg   = $var3;
} else {
    $var1  = 'no image'; 
    $var2  = 'no class'; 
    $msg   = $var3;
};

echo $msg;

I want to be able to show $var3 with the modified results but it does not work... Thank you for your answers but i think i must clarify

Sorry about the confusion..

The problem is not with the value to test ($E == 0).. My problem is that in both case the result will be the same and $var1 and $var2 will not change...

result in both cases will be the same: echo $msg; will produce "This image has this class assigned: class "

I think is because of the composing of $var3 outside the if and cannot be changed from inside IF statement..

2
  • Formating your code would be helpful. Commented Mar 7, 2011 at 16:15
  • if you are checking to see if number has a value in the POST then you need to use empty() Commented Mar 7, 2011 at 16:16

6 Answers 6

3

Maybe something like this:

if($_POST['number']) {
    $var1 = 'image';
    $var2 = 'class';
} else {
    $var1 = 'no image';
    $var2 = 'no class';
}

echo 'This ' . $var1 . ' has this class assigned: ' . $var2;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this will work but not as i want. I need to use a 3rd variable and to reduce the ammount of code
Just replace echo with $var3 like this -> $var3 = 'This ' . $var1 . ' has this class assigned: ' . $var2; and it should work like you want.
2

you have to write a function

function var3($v1,$v2)
{
   return 'This '.$v1.' has this class assigned:'.$v2;
}

use this function that way:

$msg='';
$var1='image';
$var2='class';
$var3=var3($var1,$var2);
if($_POST['number']) {$msg=$var3;} 
    else 
        {
         $var1='no image'; 
         $var2='no class'; 
         $msg=var3($var1,$var2);
         };

echo $msg;

2 Comments

You have a typo in the function var3().. You are using $1 and $2 instead of $v1 and $v2.
This is the way i ended up. It was obvious but the answer eluded me :) Thank you all! for helping
0

use this if condition:

if (isset($_POST['number']) AND strlen($_POST['number'])) {
...

Comments

0

Use something like this:

if(empty($_POST['number'])) {
  $var1='no image'; 
  $var2='no class'; 
} else {
  $var1='image';
  $var2='class';
}

echo sprintf('This %s has this class assigned:%s', $var1, $var2);

sprintf is fun! (and useful)

Comments

0

You can use sprintf to achieve what you want :

$var1='image';
$var2='class';
$format ='This %s has this class assigned: %s';

if(! isset($_POST['number']) || ! $_POST['number']) {
    $var1='no image'; 
    $var2='no class'; 
};

$msg = sprintf($format, $var1, $var2);

I took the liberty to improve a little your condition too.

Comments

0

here the solution for your problelem

$msg='';

$var1='image';

$var2='class';

$var3='This '.$var1.' has this class assigned:'.$var2;

if($_POST['number']) {

$msg=$var3;

} else {

$var1='no image'; 

$var2='no class'; 

$var3='This '.$var1.' has this class assigned:'.$var2;

$msg=$var3;

};

echo $msg;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.