DEV Community

kostaNovak
kostaNovak

Posted on

EP 5 - Conditionals and Booleans

So interesting lesson where we touch again on string interpolation and introduce booleans and conditionals.

I solved task myself first by including conditional logic inside h1 like this:

<body>

<?php
$name = "Dark Matter";

$read = true;
?>

<h1>

<?php
if ($read){
echo "You have read $name";
}else {
echo "You have NOT read $name";
}
?>
</h1>
</body>
Enter fullscreen mode Exit fullscreen mode

Jeffrey did it afterwards in more effective way I think with separating conditional logic and putting it inside first php tag before h1 and also by introducing $message variable like this :

<body>
<?php

$name = "Dark Matter";
$read = false;

if ($read){
$message = "You have read $name";
}else {
$message = "You have NOT read $name";
}
?>

<h1>
<?php
echo $message;
?>
</h1>
</body>
Enter fullscreen mode Exit fullscreen mode

So basically we are only echoing out our variable inside h1 tag and keeping conditional logic in separate php tag. More elegant solution I guess.

Top comments (0)