Bun S.
Bun S.

Reputation: 111

Unorder print out of echo in php

I don't get why does this happen? In my php code I'm trying to echo Job status that retrieve database from MySQL with a function newjob. It prints Old*Job Status: instead of Job Status: Old. Do client side and server side programming run at the same time or they have order to compile?

echo '<b>'.'Job Status: '.'</b>' .newjob($row['new_job'])."<br />";

function newjob($num) {
if($num == 1) {
echo "New";
}else{
    echo "Old";
    }
}

Upvotes: 2

Views: 80

Answers (5)

Homer6
Homer6

Reputation: 15159

This style of programming could be avoided with an ORM such as Propel. Instead of creating a standalone function, you can create another accessor that can return the "Old" or "New" string based on the state of the "Job" object.

That way, you can use the string getNewDescription() (the method that you have there) or boolean isNew() and it will be driven from the same data.

Upvotes: 1

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

This should do it:

echo '<b>Job Status:</b> ', newjob($row['new_job']), '<br>';
function newjob($num) {
    return ($num ? 'New' : 'Old');
}

Upvotes: 1

daker
daker

Reputation: 3540

echo '<b>'.'Job Status: '.'</b>' .newjob($row['new_job'])."<br />";

function newjob($num) {
if($num == 1) {
return "New";
}else{
    return "Old";
    }
}

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157889

NEVER make your function output a value.

But only return it.

echo '<b>'.'Job Status: '.'</b>' .newjob($row['new_job'])."<br />";

function newjob($num) {
    if($num == 1) {
        return "New";
    }else{
        return "Old";
    }
}

Upvotes: 8

Sampson
Sampson

Reputation: 268364

Your function needs to return rather than echo. When your function gets called, the first line expects it to return a value that can be concatenated, but the function doesn't return anything, it merely echos on invocation.

function newJob( $num ) {
    return $num == 1 ? "New" : "Old";
}

Upvotes: 2

Related Questions