Simple PHP IF/ELSE code not working properly with html markup [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2
















This question already has an answer here:




  • The 3 different equals

    5 answers




I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:






<?php 
$msg = 0;
echo $msg;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>





The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:



0



5



Tried everything I knew and searched a lot without any results. What am I doing wrong here?










share|improve this question













marked as duplicate by Funk Forty Niner html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    YOU missed = in your if condition. it should be ==.

    – Hamza Zafeer
    Nov 16 '18 at 13:28













  • Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use ===

    – Klors
    Nov 16 '18 at 13:35


















-2
















This question already has an answer here:




  • The 3 different equals

    5 answers




I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:






<?php 
$msg = 0;
echo $msg;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>





The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:



0



5



Tried everything I knew and searched a lot without any results. What am I doing wrong here?










share|improve this question













marked as duplicate by Funk Forty Niner html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    YOU missed = in your if condition. it should be ==.

    – Hamza Zafeer
    Nov 16 '18 at 13:28













  • Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use ===

    – Klors
    Nov 16 '18 at 13:35














-2












-2








-2









This question already has an answer here:




  • The 3 different equals

    5 answers




I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:






<?php 
$msg = 0;
echo $msg;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>





The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:



0



5



Tried everything I knew and searched a lot without any results. What am I doing wrong here?










share|improve this question















This question already has an answer here:




  • The 3 different equals

    5 answers




I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:






<?php 
$msg = 0;
echo $msg;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>





The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:



0



5



Tried everything I knew and searched a lot without any results. What am I doing wrong here?





This question already has an answer here:




  • The 3 different equals

    5 answers







<?php 
$msg = 0;
echo $msg;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>





<?php 
$msg = 0;
echo $msg;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>






php html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 13:25









Jo.joeJo.joe

72




72




marked as duplicate by Funk Forty Niner html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Funk Forty Niner html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4





    YOU missed = in your if condition. it should be ==.

    – Hamza Zafeer
    Nov 16 '18 at 13:28













  • Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use ===

    – Klors
    Nov 16 '18 at 13:35














  • 4





    YOU missed = in your if condition. it should be ==.

    – Hamza Zafeer
    Nov 16 '18 at 13:28













  • Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use ===

    – Klors
    Nov 16 '18 at 13:35








4




4





YOU missed = in your if condition. it should be ==.

– Hamza Zafeer
Nov 16 '18 at 13:28







YOU missed = in your if condition. it should be ==.

– Hamza Zafeer
Nov 16 '18 at 13:28















Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use ===

– Klors
Nov 16 '18 at 13:35





Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use ===

– Klors
Nov 16 '18 at 13:35












4 Answers
4






active

oldest

votes


















1














This is a simple syntax error, it needs to be:



<?php 
$msg = 0;
echo $msg;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>


Notice the == which compares things vs. = which sets things.






share|improve this answer































    1














    The = sign is for assignment; in order to do a comparison you need to use ==:



    <?php if ($msg == 5){ ?>





    share|improve this answer































      0














      The problem is in the condition -> ($msg = 5)
      It should be ==
      ($msg == 5) instead of ($msg = 5).



      When you are comparing two values you need to use double equal sign not single.
      Single means you are assigning value to the variable.



      Don'g give up!!



      Good luck!






      share|improve this answer



















      • 1





        Hi, we normally just comment on a typo and close the question. They are almost never any use to others

        – RiggsFolly
        Nov 16 '18 at 13:32





















      0

















      <?php 
      $msg = 0;
      echo $msg;
      ?>

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <title>Test</title>
      </head>
      <body>
      <?php if ($msg == 5){ ?>
      <h1>5</h1>
      <?php } elseif ($msg == 0) { ?>
      <h1> 0 </h1>
      <?php } else { ?>
      <h1>Whatever</h1>
      <?php } ?>
      </body>
      </html>








      share|improve this answer



















      • 3





        Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.

        – Nicky Mattsson
        Nov 16 '18 at 13:59


















      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      This is a simple syntax error, it needs to be:



      <?php 
      $msg = 0;
      echo $msg;
      ?>

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <title>Test</title>
      </head>
      <body>
      <?php if ($msg == 5){ ?>
      <h1>5</h1>
      <?php } elseif ($msg == 0) { ?>
      <h1> 0 </h1>
      <?php } else { ?>
      <h1>Whatever</h1>
      <?php } ?>
      </body>
      </html>


      Notice the == which compares things vs. = which sets things.






      share|improve this answer




























        1














        This is a simple syntax error, it needs to be:



        <?php 
        $msg = 0;
        echo $msg;
        ?>

        <!DOCTYPE html>
        <html lang="en">
        <head>
        <title>Test</title>
        </head>
        <body>
        <?php if ($msg == 5){ ?>
        <h1>5</h1>
        <?php } elseif ($msg == 0) { ?>
        <h1> 0 </h1>
        <?php } else { ?>
        <h1>Whatever</h1>
        <?php } ?>
        </body>
        </html>


        Notice the == which compares things vs. = which sets things.






        share|improve this answer


























          1












          1








          1







          This is a simple syntax error, it needs to be:



          <?php 
          $msg = 0;
          echo $msg;
          ?>

          <!DOCTYPE html>
          <html lang="en">
          <head>
          <title>Test</title>
          </head>
          <body>
          <?php if ($msg == 5){ ?>
          <h1>5</h1>
          <?php } elseif ($msg == 0) { ?>
          <h1> 0 </h1>
          <?php } else { ?>
          <h1>Whatever</h1>
          <?php } ?>
          </body>
          </html>


          Notice the == which compares things vs. = which sets things.






          share|improve this answer













          This is a simple syntax error, it needs to be:



          <?php 
          $msg = 0;
          echo $msg;
          ?>

          <!DOCTYPE html>
          <html lang="en">
          <head>
          <title>Test</title>
          </head>
          <body>
          <?php if ($msg == 5){ ?>
          <h1>5</h1>
          <?php } elseif ($msg == 0) { ?>
          <h1> 0 </h1>
          <?php } else { ?>
          <h1>Whatever</h1>
          <?php } ?>
          </body>
          </html>


          Notice the == which compares things vs. = which sets things.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 13:28









          AdamAdam

          1,05121520




          1,05121520

























              1














              The = sign is for assignment; in order to do a comparison you need to use ==:



              <?php if ($msg == 5){ ?>





              share|improve this answer




























                1














                The = sign is for assignment; in order to do a comparison you need to use ==:



                <?php if ($msg == 5){ ?>





                share|improve this answer


























                  1












                  1








                  1







                  The = sign is for assignment; in order to do a comparison you need to use ==:



                  <?php if ($msg == 5){ ?>





                  share|improve this answer













                  The = sign is for assignment; in order to do a comparison you need to use ==:



                  <?php if ($msg == 5){ ?>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 13:28









                  TordekTordek

                  7,16122961




                  7,16122961























                      0














                      The problem is in the condition -> ($msg = 5)
                      It should be ==
                      ($msg == 5) instead of ($msg = 5).



                      When you are comparing two values you need to use double equal sign not single.
                      Single means you are assigning value to the variable.



                      Don'g give up!!



                      Good luck!






                      share|improve this answer



















                      • 1





                        Hi, we normally just comment on a typo and close the question. They are almost never any use to others

                        – RiggsFolly
                        Nov 16 '18 at 13:32


















                      0














                      The problem is in the condition -> ($msg = 5)
                      It should be ==
                      ($msg == 5) instead of ($msg = 5).



                      When you are comparing two values you need to use double equal sign not single.
                      Single means you are assigning value to the variable.



                      Don'g give up!!



                      Good luck!






                      share|improve this answer



















                      • 1





                        Hi, we normally just comment on a typo and close the question. They are almost never any use to others

                        – RiggsFolly
                        Nov 16 '18 at 13:32
















                      0












                      0








                      0







                      The problem is in the condition -> ($msg = 5)
                      It should be ==
                      ($msg == 5) instead of ($msg = 5).



                      When you are comparing two values you need to use double equal sign not single.
                      Single means you are assigning value to the variable.



                      Don'g give up!!



                      Good luck!






                      share|improve this answer













                      The problem is in the condition -> ($msg = 5)
                      It should be ==
                      ($msg == 5) instead of ($msg = 5).



                      When you are comparing two values you need to use double equal sign not single.
                      Single means you are assigning value to the variable.



                      Don'g give up!!



                      Good luck!







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 13:31









                      Marcin - user2676388Marcin - user2676388

                      564




                      564








                      • 1





                        Hi, we normally just comment on a typo and close the question. They are almost never any use to others

                        – RiggsFolly
                        Nov 16 '18 at 13:32
















                      • 1





                        Hi, we normally just comment on a typo and close the question. They are almost never any use to others

                        – RiggsFolly
                        Nov 16 '18 at 13:32










                      1




                      1





                      Hi, we normally just comment on a typo and close the question. They are almost never any use to others

                      – RiggsFolly
                      Nov 16 '18 at 13:32







                      Hi, we normally just comment on a typo and close the question. They are almost never any use to others

                      – RiggsFolly
                      Nov 16 '18 at 13:32













                      0

















                      <?php 
                      $msg = 0;
                      echo $msg;
                      ?>

                      <!DOCTYPE html>
                      <html lang="en">
                      <head>
                      <title>Test</title>
                      </head>
                      <body>
                      <?php if ($msg == 5){ ?>
                      <h1>5</h1>
                      <?php } elseif ($msg == 0) { ?>
                      <h1> 0 </h1>
                      <?php } else { ?>
                      <h1>Whatever</h1>
                      <?php } ?>
                      </body>
                      </html>








                      share|improve this answer



















                      • 3





                        Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.

                        – Nicky Mattsson
                        Nov 16 '18 at 13:59
















                      0

















                      <?php 
                      $msg = 0;
                      echo $msg;
                      ?>

                      <!DOCTYPE html>
                      <html lang="en">
                      <head>
                      <title>Test</title>
                      </head>
                      <body>
                      <?php if ($msg == 5){ ?>
                      <h1>5</h1>
                      <?php } elseif ($msg == 0) { ?>
                      <h1> 0 </h1>
                      <?php } else { ?>
                      <h1>Whatever</h1>
                      <?php } ?>
                      </body>
                      </html>








                      share|improve this answer



















                      • 3





                        Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.

                        – Nicky Mattsson
                        Nov 16 '18 at 13:59














                      0












                      0








                      0










                      <?php 
                      $msg = 0;
                      echo $msg;
                      ?>

                      <!DOCTYPE html>
                      <html lang="en">
                      <head>
                      <title>Test</title>
                      </head>
                      <body>
                      <?php if ($msg == 5){ ?>
                      <h1>5</h1>
                      <?php } elseif ($msg == 0) { ?>
                      <h1> 0 </h1>
                      <?php } else { ?>
                      <h1>Whatever</h1>
                      <?php } ?>
                      </body>
                      </html>








                      share|improve this answer
















                      <?php 
                      $msg = 0;
                      echo $msg;
                      ?>

                      <!DOCTYPE html>
                      <html lang="en">
                      <head>
                      <title>Test</title>
                      </head>
                      <body>
                      <?php if ($msg == 5){ ?>
                      <h1>5</h1>
                      <?php } elseif ($msg == 0) { ?>
                      <h1> 0 </h1>
                      <?php } else { ?>
                      <h1>Whatever</h1>
                      <?php } ?>
                      </body>
                      </html>








                      <?php 
                      $msg = 0;
                      echo $msg;
                      ?>

                      <!DOCTYPE html>
                      <html lang="en">
                      <head>
                      <title>Test</title>
                      </head>
                      <body>
                      <?php if ($msg == 5){ ?>
                      <h1>5</h1>
                      <?php } elseif ($msg == 0) { ?>
                      <h1> 0 </h1>
                      <?php } else { ?>
                      <h1>Whatever</h1>
                      <?php } ?>
                      </body>
                      </html>





                      <?php 
                      $msg = 0;
                      echo $msg;
                      ?>

                      <!DOCTYPE html>
                      <html lang="en">
                      <head>
                      <title>Test</title>
                      </head>
                      <body>
                      <?php if ($msg == 5){ ?>
                      <h1>5</h1>
                      <?php } elseif ($msg == 0) { ?>
                      <h1> 0 </h1>
                      <?php } else { ?>
                      <h1>Whatever</h1>
                      <?php } ?>
                      </body>
                      </html>






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 13:38









                      Rm khanRm khan

                      92




                      92








                      • 3





                        Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.

                        – Nicky Mattsson
                        Nov 16 '18 at 13:59














                      • 3





                        Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.

                        – Nicky Mattsson
                        Nov 16 '18 at 13:59








                      3




                      3





                      Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.

                      – Nicky Mattsson
                      Nov 16 '18 at 13:59





                      Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.

                      – Nicky Mattsson
                      Nov 16 '18 at 13:59



                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values