Jump to content

[SOLVED] if and else


jmr3460

Recommended Posts

conditions are not terminated by semicolons.  Conditions  use { } brackets to mark the beginning and end of what you want executed if they evaluate true.  But, if you are only wanting to execute one expression if it evaluates true, you don't need the { } brackets.  Examples:

 

if (condition)
  expression1; // will be executed if condition is true

 

if (condition) {
  expression1; // will be executed if condition is true, same as first one
}

 

if (condition)
  expression1; // will be executed if condition is true
  expression2; // will be executed regardless of whether condition is true or not, as it is not tied to the condition

 

if (condition) {
  expression1; // will be executed if condition is true
  expression2; // will be also only be executed if condition is true, since it is wrapped inside the brackets
}

 

So technically, you could have written your code like this:

<?php
if(setcookie("pam","Pamela is great"))
  echo "The deal is done!";
else
  echo "No cookie for you!";
?>

Link to comment
https://forums.phpfreaks.com/topic/160429-solved-if-and-else/#findComment-846617
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.