Sunday 2 March 2014

Global Variables And Functions In Php

Global Variables
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):-

Code to see IP address
Screenshot1


<?php



echo $user_ip = $_SERVER['REMOTE_ADDR'];



?>

Output (Screenshot2):-

IP address
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):-

Your IP address
Screenshot3




<?php



$user_ip = $_SERVER['REMOTE_ADDR'];



echo $string = 'Your IP address is: '.$user_ip;



?>

Output (Screenshot4):-

IP address
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):-

See ip address
Screenshot5


<?php

$user_ip = $_SERVER['REMOTE_ADDR'];



function echo_ip() {

$string = 'Your IP address is:'.$user_ip;

echo $string;

}

echo_ip();

?>

output (Screenshot6):-

IP address
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):-

Users Ip address
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):-

IP address global variables
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):-

ip
Screenshot9



<?php



function echo_ip() {

$user_ip = $_SERVER['REMOTE_ADDR'];

$string = 'Your IP address is:'.$user_ip;

echo $string;

}

echo_ip();



?>

Output (Screenshot10):-

See Ip address
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..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates