PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

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


This is done using the global keyword. Here is an example that you can copy and paste to see what happens when we try to use a variable within a function without using the global keyword and then when we do:

<?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.

The '@' before the text in nothing supresses a PHP Notice error when it tries to echo an undefined variable.



ASK A QUESTION

More functions PHP Questions

When running a php script, is it possible to have it pull from one txt. file and then a 2nd txt. file?

How to verify that a function exists before you try using it.

Can we use include ("abc.php") two times in PHP page "makeit.php" ?

I am currenly trying to translate a language-file (english.php to norwegian.php) But I have run across something strange: In the middle of the file, there is a line with: ?>

I have used the funtion Rate (code below) and i have some troubles with it. When i use it with the following values: (Wrong) Excel - RATE(228;-196,02;-49005;961546,464646;1;0,05) gives: 0,0119 Php - RATE(228,-196.02,-49005,961546.464646,1,0.05) gives



Custom Search