top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I make a variable from outside a function work in that function?

0 votes
343 views
How can I make a variable from outside a function work in that function?
posted Jul 8, 2014 by Amanpreet Kaur

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

This is done using the global keyword. Here is an example
<?php $data = "Nothing to declare"; function nothing() { echo @"I have $data"; } function todeclare() { global $data; echo "I have $data"; } nothing(); echo "<br />"; todeclare(); ?>

This will print on the first line:
'I have'

and then on the next line

'I have Nothing to declare'

This illustrates that the global keyword is needed to allow the function to access its value from outside the function.

answer Jul 10, 2014 by Mohit Sharma
Similar Questions
+2 votes

Is there any way so that i will get to know about my variable is a number or not without using Javascript?

+3 votes

Say I have sent a command to a process using the fwrite command something like

...
while (true) {
 switch (expect_expectl($stream, $cases)) {
 case "ls":
 fwrite($stream, "ls -aln");
 break;
..

Is there a way I can save the output from that process to a variable ?

+1 vote

I'm writing my first php extension and I need to list included files (in PHP script) from RINIT function, but I cannot figure out how.

I deep into PHP source code and I think it's related to EG(included_files), but I can't to access the list.

PHP_RINIT_FUNCTION(extname)
{
 // SAPI NAME AND PHP SCRIPT FILE HANDLE PATH
 char *pt_var_sapi_name = sapi_module.name;
 char *pt_var_file_handle_path = SG(request_info).path_translated;

 // HOW CAN I USE EG(included_files) to get included files list?

 return SUCCESS;
}
...