0

why this function display sharma not love on browser

<?php
$a='abc';
define("h",sharma);
define("h",love);
echo h;
?>

and second

why this will show nothing on browers?
<?php 
$a='abc';
bb();
function bb()
{
	echo $a;
}
?>
2
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by mangel.murti
0


and second

<?php 
$a='abc';
bb();
function bb()
{
	echo $a;
}
?>

In this section, the variable $a is local, and not defined in the global scope. When you try to reference $a inside the function, because it is not defined inside the function, nor passed as a variable TO the function, it reads that inside of the function, $a=''. Now, if you were to code this:

why this will show nothing on browers?
<?php 
$a='abc';
global $a;
bb();
function bb()
{
	echo $a;
}
?>

...the browser would print 'abc', because $a='abc' regardless of where it is called in the script.

NOTE: Unless otherwise necessary, I do not personally advocate the overuse of global.

0

why this function display sharma not love on browser

<?php
$a='abc';
define("h",sharma);
define("h",love);
echo h;
?>

Not sure why $a is in this example, but ok. :)

These are the differences between constants and variables:

Constants do not have a dollar sign ($) before them;
Constants may only be defined using the define() function, not by simple assignment;
Constants may be defined and accessed anywhere without regard to variable scoping rules;
Constants may not be redefined or undefined once they have been set; and
Constants may only evaluate to scalar values.

You can find the whole skinny here:

http://www.php.net/manual/en/language.constants.php

Hope this helped.

0

thnx 4 reply...

but after declaring that variable global it is not showing anything on my browers.

i have understand second answer.

Edited by mangel.murti: n/a

0

thnx 4 reply...

but after declaring that variable global it is not showing anything on my browers.

i understand second answer.

That was a misstep on my part.

<?php 

$a='abc';

bb();

function bb() {
        global $a;
	echo $a;
}
?>

The global is called where it is needed, here inside the function.

I just so rarely use them that I forget their specifics sometimes. Sorry about that. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.