Conditional functions in PHP


<?php

$makefoo = true;

/* We can’t call foo() from here
since it doesn’t exist yet,
but we can call bar() */

bar();

if ($makefoo) {
echo “First “;
function foo()
{
echo “I don’t exist until program execution reaches me.\n”;
}
echo “Second “;
}

/* Now we can safely call foo()
since $makefoo evaluated to true */

if ($makefoo) foo();
function bar()
{
echo “I exist immediately upon program start.\n”;
}

?>

Check and execute this program and try to modify

Execution steps

First we make the $makefoo as true. and now the function bar(); is called so now this executes the script in bar

Next the execution comes to the if statement on  if ($makefoo) foo(); now this makefoo is true it directly executes the first and second statements . Now the exection comes to the if statement On executing this foo function the condition is true so then the script is and the output comes like this

I exist immediately upon program start. First Second I don’t exist until program execution reaches me.

Note: Also check with foo(); function on top and execute what happens and why it happens

About pacesettergraam

A good and realistic person

Posted on April 24, 2012, in coding-web development. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: