In php,AFAIK dollarsign($) is part of variable thatswhy we always variable using this sign........ Now,whats wrong when i use the above defination while accessing a member variable of a class using object, for instance <?php class hello { public $a="php seems sometime illogical"; function _construct() { echo "howz your php experience?"; } } $obj=new hello(); $obj->a;//this works fine $ob->$a;//error::can,t acess empty property? 2)does function are define at the point they call in php? why its necessary to call inner function first before outer function in nested function?
$ob->$a; these are needed just for static variables ! if your variable is not static you dont need a $ sign before 'a'. and i cant answer 2nd question because I am also just a beginner in php
thankz mate but here omitting dollar sign become compulsary since i am getting error.. and why its showing different behaviour with static variables?
Correct. When you use $obj->a you are accessing the PROPERTY of the object as referenced inside the object as $a..... In the latter, however, you are passing in a VARIABLE as the PROPERTY. Consider this. <?php class hello { public $a="world"; public $world="hello"; function _construct() { echo "howz your php experience?"; } } $obj=new hello(); $a = $obj->a; print $obj->$a; PHP: Output: hello This is because I have passed in the value of $a (world) and accessed $obj->world because of it (and this is why it is correctly reporting that its an empty property) Im not sure i understand what you are asking there?
lukeg32 this defination clear my concept...thankz mate well i try to make my 2nd ques more understandable <?php function foo() { function bar() { echo "you have to first call foo()"; } } PHP: in these nested function why we need to call first foo() and then only we can call bar()?
Ahhh right, I understand you now. Yes, the inner function will only be accessible once you call the outer function for the first time; I dont think that functionality is terribly useful and you should also bear in mind that if you call the outer function more than once, it will throw an error about not redeclaring the (inner) function
i think if we call outer function more than once it will throw an error for declaring the inner function more than once...i mean whenver we call outer function inner function get declare and may be thats why first we need to call outer function....i may be wrong
You can call the inner function as often as you like; its only when you call the outer function it will error, since it is trying to redeclare the function.... E.G <?php function foo() { function bar() { print "you have to first call foo()"; } } print bar(); # This will error as its undefined print foo(); # This will be fine (no output) print bar(); # This will be fine print bar(); # This will be fine print foo(); # This will error as redeclared function ?> PHP:
well i come from c++ ground.. so in c++ i read that we can declare a variable or function as many time we want with maintaining one defination rule. is there any difference in declaration and defination in php?if there is then plz let me know
Im not sure i follow again... sorry, maybe its been a long day - can you give another example? PHP is a higher level than C so there are similarities.... indeed, there are compilers that will convert it to C->binary .... but i cant answer your specific question until I understand it :/
In c++, int sum(int a, int b);//since this is declaration , i can write this line several times in my program.. int sum(int a, int b){}//this is defination,i can,t write defination of one function more than once else it ll lead to linker error means in c/c++ declaration determine scope,type,lifetime of variable while defination determine the memory required by variable in php, its giving error on redeclaration , as you said why its so?