Global Variables and functions inside Php Programming languages.In this tutorial we will go through global variables and its use inside functions. Now you can come across the problem that when you predefined variables outside of your functions and you try to use them within your functions, you may find that you are not getting any response from the variable and thus can’t do any thing with them.
Code to see the IP address (Screenshot1):-
|
Screenshot1 |
<?php
echo $user_ip = $_SERVER['REMOTE_ADDR'];
?>
Output (Screenshot2):-
|
Screenshot2 |
In this example we are echoing out user’s IP address. Don’t worry if you don’t understand, this code returns the user’s IP address. Here, variable $_SERVER presets the environment variables that we can use. So, Let echo this out. Now because I am at localhost and since am working on localserver so my computer’s IP address is just 127.0.0.1 which is standard notation for a localhost server.
Code for see the ip address(Screenshot3):-
|
Screenshot3 |
<?php
$user_ip = $_SERVER['REMOTE_ADDR'];
echo $string = 'Your IP address is: '.$user_ip;
?>
Output (Screenshot4):-
|
Screenshot4 |
In this example, I am echoing out IP address by storing it in $string variable and then echo out IP address as 127.0.0.1. Now let see this with functions
Code for see the ip address in functions(Screenshot5):-
|
Screenshot5 |
<?php
$user_ip = $_SERVER['REMOTE_ADDR'];
function echo_ip() {
$string = 'Your IP address is:'.$user_ip;
echo $string;
}
echo_ip();
?>
output (Screenshot6):-
|
Screenshot6 |
In this example I have created function to echo out IP address of mylocalhost. Now you will notice that in output its not reference variable $string to refer variable $user_ip because $user_ip is outside the function and $string is inside the function. Its not referencing the variable to $string therefore IP address of our localhost is not echoing out.Actually we can’t able to access variable $user_ip to the function. Now let see how can we solve to access our variable to the functions.
Code for see the ip address in functions (Screenshot7):-
|
Screenshot7 |
<?php
$user_ip = $_SERVER['REMOTE_ADDR'];
function echo_ip() {
global $user_ip;
$string = 'Your IP address is:'.$user_ip;
echo $string;
}
echo_ip();
?>
Output (Screenshot8):-
|
Screenshot8 |
In this example we are accessing the variable from outside of our functions with keyword “global” it will reference the variable inside the functions. In this example you can see we can use variable $user_ip to reference to $string inside the functions.
Now question is why to do all this ??
Answer is very simple:- You may have many static and dynamic variables outside functions.
Code for ip address inside functions (Screenshot9):-
|
Screenshot9 |
<?php
function echo_ip() {
$user_ip = $_SERVER['REMOTE_ADDR'];
$string = 'Your IP address is:'.$user_ip;
echo $string;
}
echo_ip();
?>
Output (Screenshot10):-
|
Screenshot10 |
In this example you can see we define variables inside the functions and we don’t use global keyword because we don’t need of global keyword variables are inside function we don’t want to reference them from outside the functions.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..