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 Functions Introducing Functions Introducing Functions

Vidit Shah
Vidit Shah
6,037 Points

What's wrong with my php code?

<?php
function hello() {
    echo "hello,world!";
}
hello();


//This code Doesnt work
$current_user = "Vidit";
function check(){
    global $current_user;
    if($current_user=="Vidit")
    {
        echo "Hey! This is Vidit";
    }

    else {
        echo "Sorry!";
    }
}


?>

my second function is not working

Andrew Shook
Andrew Shook
31,709 Points

What exactly isn't working?

Niclas Valentiner
Niclas Valentiner
8,947 Points

I don't see a function call for check(). It's either not working because you didn't call the function or there is more code you haven't shared which contains the actual problem.

1 Answer

What was stated above is correct, you aren't calling your check function in your code, you need:

<?php
function hello() {
    echo "hello,world!";
}

hello();

$current_user = "Vidit";

function check() {
    global $current_user;
    if($current_user=="Vidit") {
        echo "Hey! This is Vidit";
    } else {
        echo "Sorry!";
    }
}

check();
?>

With that in place you will see "hello,world!Hey! This is Vidit" on your screen.