Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Conditionals & Loops Conditionals Challenge

Ezra John Alvarez
Ezra John Alvarez
557 Points

Can u use variables inside condition? or you can only use constants?

Can u use variables inside php conditions like:

<?php
$name = "Mike";


if($name="Mike") {
echo "Great Mike";
}


?>

2 Answers

Absolutely, you have to use a comparison. Think of it as a single '=' sign assigns a variable, '==' and '===' compare values.

I think you'll learn more about those in this course, but your code above would look like

<?php

$name = "Mike";

if ($name == "Mike") {
    echo "Great Mike"; 
}

?>

I've also edited your code above so you can see how to markup code in the forum

Yes, you can but it would be a double equal sign between your variable and value as you are looking for 'is Equal to'.