Why is the foo() function executed while not having been called? - php

I'm kind of new to PHP so please excuse the simplicity of my question in case it was,
I have a function foo() that always returns true, now my question is that while I have only checked the true being of foo() and not called the foo() function directly, how possibly did it execute then?
And is only PHP like this or it is the same say in JavaScript?
$x = 10;
function foo() {
global $x;
if($x = 10) {
return true;
}
return false;
}
if(foo()) {
echo 'Done foo() function!';
} else {
echo 'Not done foo() function...';
}

you did called foo in if(foo()) this line. Essentially what you did in this line is called foo and checked its return value with if. If you want to check if foo function is executed or not you can keep a flag as global variable and and set it to true in the function somewhere like this
$foo_executed = false;
function foo (){
global $foo_executed;
// your code here
$foo_executed = true;
}
//execute foo
foo();
if ($foo_executed){
echo "foo executed";
}
This behavior is also common in other programming languages. You can follow this tutorial to learn more about functions. good luck!
https://www.youtube.com/watch?v=HvxQww-7NGA

Related

function call from another function in php and php script should run backgound

I have created a class A() with two functions i.e a() and b().
Function a() have a condition if(state==true) then break operation.
If the condition is false, then it should call function b().
Function b() will call again function a() untill function's a() condition gets true.
Below is my code :
class name
{
public function a($id)
{
if() {} else {
b($id);
}
}
public function b($id)
{
sleep(10);
a();
}
}
$id = 4;
$oba = new name();
$oba->a($id);
hi hussain are you wanted to do something like this?
<?php
class name{
public function a($id)
{
if($id==4){
echo"condition statisfied";
return true;
}
}
}
$id= 0;
$a_obj = new name();
while ( $id<= 4) {
$a_obj->a($id);
echo "id count :".$id."<br>";
$id++;
}
?>
this is the efficeint way to perform what you wanted to do.
and for running script in background you can refer this link
and please explain question properly.
soo far you don't have a function called a() as your taking it as a constructor.

Validity of php variables

It may sound silly but I'm quite php outdated/unexperienced and coming from Java programming back to php, so I mix up the concepts.
If in a php webpage I declare a variable
$debug=TRUE;
and try to access it from below within a function
a(){
if ($debug){
echo "I'm here";
}
}
the variable doesn't exists or isn't initiated? The whole file is just simply:
<?php
$debug=TRUE;
a(){
if ($debug){
echo "I'm here";
}
}
?>
Do I need to make a session variable or something else? I'm quite clueless & the same concept is confusing me for the use of other variables within. Also for the further use of variables, I am trying to be forced to pass all the variables I need forward to the function where I use them and a class concept as in Java perhaps would be cleaner but is a kind of too much for this simplicity. Or do I need the functions (it's a form processor) to be declared as a class?
I know this is silly, but I looked through Google and forums and the problem seems to be so obvious and simple that it's hard to find a webpage or entry targeting this (or perhaps I'm asking the wrong question).
http://php.net/manual/en/language.variables.scope.php
<?php
$debug = TRUE;
function a() {
global $debug;
if($debug === TRUE) {
echo "I'm here....\n"
}
}
Better, instead of using globals you can pass it in as a parameter:
<?php
$debug = TRUE;
function a($debug = TRUE) {
if($debug === TRUE) ....
}
You can also use the $_GLOBALS array:
<?php
$debug = TRUE;
function a() {
if($_GLOBALS['debug'] === TRUE) ...
}
You can use constants, which are always in scope:
<?php
define('DEBUG', TRUE);
function a() {
if(DEBUG === TRUE) ...
}
You can also use a singleton:
function a() {
if(SingletonClass::get_instance()->debug === TRUE) {
...
}
}
You'll have to create a singleton class which extends StdClass() to get implicit ->get and ->set methods.
http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html
did you want something like this:
<?php
$debug=TRUE;
function a($debug){
if ($debug){
echo "I'm here";
}
}
a($debug);//outputs "I'm here"
?>
A few things here a(){} isn't defined as a function
function a(){
}
Next, you shouldn't try use globals unless you absolutely want them. But, you could
$debug = TRUE;
function a(){
global $debug;
if($debug)
{
echo "it is";
}
}
then call a() whenever you want to check it.
I must say I don't think this is a great practice in how you are trying to debug.
Pass variables to a function, like this:
$debug = true;
function a($debug) {
var_dump($debug);
}
a($debug);
// Outputs bool(true)

Call function within a function PHP

1 function foo($i){
2 return bar($i)*4;
3 function bar($i){
4 return $i*4;
5 }
6 }
7 echo foo(4);
return
Fatal error: Call to undefined function bar() in /var/www/index.php on line 2
why doesn't it work? it works well in javascript, while it works when i do this:
function foo($i){
return bar($i)*4;
}
function bar($i){
return $i*4;
}
Define the function above your return value, otherwise it never gets executed.
<?php
function foo($i){
function bar($i){
return $i*4;
}
return bar($i)*4;
}
echo foo(4);
?>
It doesn't work as you are calling bar() before it has been created.
See example 2 here:- http://www.php.net/manual/en/functions.user-defined.php
Your code never reach function bar(...), therefore it's never defined.
You need to put your bar() function before your foo() or before the return bar. (Answer based on your first example).
With your second example you probably define bar() before you use foo() therefore bar() is defined and it works fine.
So as long as you have your function defined when you hit a certain spot in your code it works fine, no matter if it's called from within another function.
If you define function within another function, it can be accessed directly, but after calling parent function.
For example:
function a () {
function b() {
echo "I am b.";
}
echo "I am a.<br/>";
}
//b(); Fatal error: Call to undefined function b() in E:\..\func.php on line 8
a(); // Print I am a.
b(); // Print I am b.
Execution not execute after return statement
change your code like this
function foo($i){
function bar($i){
return $i*4;
}
return bar($i)*4;
}
echo foo(4);
For the sake of having a "recent" answer, here's what I did:
function printRx($conn, $patient)
{
function rows($rows, $tod)
{
for ($x = 0; $x < count($tod); $x++) {
if ($tod[$x] == 1) {
$rows++;
break;
}
}
return $rows;
}
$tod['prn'] = (explode('|', $row['prn'])) ?? null;
$rows = rows($rows, $tod['prn']);
return;
}
Works beautifully

Defining a function inside a conditional

Snippet 1 works. Snippet 2 doesn't. Why?
1.
foo();
function foo()
{
// do soemething
}
2.
foo();
if(!function_exists("foo"))
{
function foo()
{
// do soemething
}
}
See http://www.php.net/manual/en/functions.user-defined.php:
Functions need not be defined before
they are referenced, except when a
function is conditionally defined [...]
Its definition must be processed prior
to being called.
You're trying to execute foo() before testing to see whether it's defined or not (and subsequently defining it)
if(!function_exists("foo"))
{
function foo()
{
// do soemething
}
}
foo();

Is it possible to access outer local variable in PHP?

Is it possible to access outer local varialbe in a PHP sub-function?
In below code, I want to access variable $l in inner function bar. Declaring $l as global $l in bar doesn't work.
function foo()
{
$l = "xyz";
function bar()
{
echo $l;
}
bar();
}
foo();
You could probably use a Closure, to do just that...
Edit : took some time to remember the syntax, but here's what it would look like :
function foo()
{
$l = "xyz";
$bar = function () use ($l)
{
var_dump($l);
};
$bar();
}
foo();
And, running the script, you'd get :
$ php temp.php
string(3) "xyz"
A couple of note :
You must put a ; after the function's declaration !
You could use the variable by reference, with a & before it's name : use (& $l)
For more informations, as a reference, you can take a look at this page in the manual : Anonymous functions
You must use the use keyword.
$bar = function() use(&$l) {
};
$bar();
In the very very old PHP 5.2 and earlier this didn't work. The syntax you've got isn't a closure, but a definition of a global function.
function foo() { function bar() { } }
works the same as:
function foo() { include "file_with_function_bar.php"; }
If you execute function foo twice, PHP will complain that you've tried to re-define a (global) function bar.
You can read default value by:
function(){
return preg_match(
"yourVar = \d+"
, str_file_get_contents(functionFile)
, arrayToPutFieldsValue
);
}
If You would use two functons in the same time - it's like someone's using a spoon and You want to take a food from that spoon - You'll waste a food or some of You will starv.
Anyway - You would have to set a pointer somehow in a hard way.
It's impossible to get any field from other function or class without calling it to life.
Functions/methods are instance-like - they need to be called.
Share the common fields by accessing a global fields with synchronized functions.
function a()
{
function val1($arg=null)
{
static $a;
if ($arg !== null) $a = $arg;
else return $a;
}
function b()
{
val1('1234');
echo val1() . '<br>'; // shows: 1234
val1('my custom data');
echo val1() . '<br>'; // shows: my custom data
}
b();
}
a();
Used val1('my custom data') to set my value
Used val1() to get my value

Categories