Jump to content

global variables


jimfog

Recommended Posts

suppose we have 2 functions with one variable declared in each one of them and 2 declared as global in (again) each of them:

function a(){global $a,$b$a}function b(){global $a,$b,$b}

Can $b "see" $a and vice versa. Some code in my script does not work and i cannot find why.

Link to comment
Share on other sites

Variables don't "see" other variables. For any code, all of the variables that are available in that piece of code are anything that was declared in the local scope, plus anything declared as explicitly global (plus superglobals). Declaring a variable as global just brings it into the local scope. So the code in those functions can see both variables but the variables themselves don't really have a scope. If your variables are objects then things are a little bit more complex with regard to visibility of the object members. Instead of declaring variables as global you can also use the $GLOBALS array to refer to them, e.g. $GLOBALS['a'].

Link to comment
Share on other sites

Variables don't "see" other variables. For any code, all of the variables that are available in that piece of code are anything that was declared in the local scope, plus anything declared as explicitly global (plus superglobals). Declaring a variable as global just brings it into the local scope. So the code in those functions can see both variables but the variables themselves don't really have a scope. If your variables are objects then things are a little bit more complex with regard to visibility of the object members. Instead of declaring variables as global you can also use the $GLOBALS array to refer to them, e.g. $GLOBALS['a'].
I am little confused.Second, the term "see" was not correct. What i am trying to say is that(given the above code) if we type in function b:
function b(){global $a,$b$b;echo $a;}

won't we also output the value of $a which is declared in function a? That was my point. You said also:

Declaring a variable as global just brings it into the local scope
You mean that declaring a variable as global is "seen" only in the local scope?Is this variable available also outside the function in which it is declared as global? We are not talking about objects here. Anyway, the above wording is a little messy-that is because i am confused-sorry for that. Maybe i will restate the problem i am facing with different words sometime later.
Link to comment
Share on other sites

won't we also output the value of $a which is declared in function a? That was my point
Assuming the other function was executed first, yes. Both functions work on the same variable, a variable that was declared in the global scope. There aren't two different versions of that variable in the two functions.
You mean that declaring a variable as global is "seen" only in the local scope? Is this variable available also outside the function in which it is declared as global?
The variable is declared in the global scope, and you're also adding it to the local scope. A global variable is available in any scope where it is declared. If that doesn't help it may be better to post specific code instead of an abstract example. Your example doesn't set the values of either variable, for instance.
Link to comment
Share on other sites

When function b() is running, it will see the values that $a and $b had when it started running. If function a() changes the variable before function b() runs, then the value of the variable will be changed in function b().

Link to comment
Share on other sites

I am going to be more specific about the problem i am facing by posting code. Not now though, cause the code is much i want to make it a little more cleaner so that you can get a meaning out of it and be able to spot the problem easier. So, give some time to do the above and i will post again here...

Link to comment
Share on other sites

As i said, i am trying to make the code more efficient and i am finding obstacles in doing so: The following code segment works:

if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");if (!isset($_REQUEST["monthcol"])) $_REQUEST["monthcol"] = date("n");$cMonth = $_REQUEST["month"];$cMonthcol=$_REQUEST["monthcol"];echo '<a href="'.$_SERVER["PHP_SELF"]. "?month= ". $prev_month ." &year= " .$prev_year. '">		    <img src="Images/prevmont_arrow.png" width="12" height="14" alt="προηγούμενος μήνας" /></a>'; echo  '<div class="monthname">'.$monthNames[$cMonth - 1].''. $cYear.'</div>';

In the code above, when the user clicks the link, the month, which is echoed in the browser and is contained in div class monhtname ,is reduced by one-it is like the calendar which is displayed in the left side of Ms-outlook.(monthcolumn is the month printed in another place in the webpage-not shown here). I want to use the following code now-shorter version, but the problem is that it does NOT WORK:

if ((!isset($_REQUEST["monthcol"]))||(!isset($_REQUEST["month"]))) $_REQUEST["month"]=$_REQUEST["monthcol"] = date("n");

The rest of the code remains unchanged in comparison with the code that is in the beginning.The problem is that the month printed in the calendar stays the same. The year changes also-the code for the year is this:

if (!isset($_REQUEST["year"]))  $_REQUEST["year"]  = date("Y");$cYear  = $_REQUEST["year"];if ($prev_month == 0 ) {    $prev_month = 12;    $prev_year = $cYear - 1;}

For example, now is February, clicking the link, does not print January, as it should( and as the earlier version of the code did)- Any ideas? Do you want me to post more code? I have tried numerous thing but none of it worked. Many thanks in advance.

Link to comment
Share on other sites

Why are you using 2 variables for the month? The problem is the or condition in that if statement. If $_REQUEST['month'] has the correct value, but $_REQUEST['monthcol'] was not sent, then it sets both values to the current month instead of using the value from $_REQUEST['month'].

Link to comment
Share on other sites

Οk, let us forget for a while the above code-the important thing is that i have managed to isolate/pinpoint the problem.Follow me and i will so you what i want to achieve: This is the link the user clicks:

 echo '<a href="'.$_SERVER["PHP_SELF"]. "?month= ". $prev_month ." &year= " .$prev_year. '">

The following code gets the month which by the user clicking the above link:

if (!isset($_GET["month"])) $_GET["month"] = date("n");$cMonth = $_GET["month"];

prev_month equals:

$prev_month = $cMonth-1;

$cMonth is "printed" here:

echo  '<div class="monthname">'.$monthNames[$cMonth - 1].''. $cYear.'</div>';

$monthNames is an array containing the the namef of the month. The outcome of the above code(all of it) is that-if for example-we have February(as is the case is now), by clicking the link the printed month will be January and so on. Now there is also this link:

<li><a href="'.$_SERVER["PHP_SELF"]. "?day= ". $prev_day ."&monthcol=".$prevc_month."&yearcol=".$cprevyear.'">Previous day</a></li>

The above link send the variables in a different GET array-i will not write this down-i believe it is not needed. What i want is that when the $prev_day reaches a certain threshold,(the 1st day of the month for example) $cMonth-1 occurs like in the code above. So far i haven not managed to do it despite numerous attempts:

if($prev_day==0){$cmonth-1;}

I tried the above without success. What are we trying to achieve here, basically, is to "simulate" user action somehow, simulating the click of the link(the first one i mention) so as to achieve $cMonth-1. I am all ears. Thanks.

Link to comment
Share on other sites

Unfortunately it did not work. The only way to change the value of $cMonth is only by clicking the link which from then, GET handles the rest. It seems, that if we try to change it as you do(and as i did before you) the result will not be what we desire. Restating the problem, i would say, is a link the only way we can send data to a GET array? Thanks for your help.

Link to comment
Share on other sites

You mean:

 If($prev_day){form goes here method=getblah blah}

Something like the above?

Link to comment
Share on other sites

He just meant that the two ways to send information to PHP via $_GET are to either manually add variables to a URL, or use a form with method="get". You process both of them the same way in PHP, the values are all inside $_GET. But you asked if a link was the only way to send information to PHP via $_GET, and the answer is that you can also use a form.

Link to comment
Share on other sites

Yes, but a form, wont require user action?

Link to comment
Share on other sites

I'm not entirely sure what you're trying to get to, GET is a way for a user to send data to the server. If you don't want the user involved then GET is not what you're looking for.Are you trying to store a value across multiple pages?

Link to comment
Share on other sites

The user at SOME POINT gets involved, it is the point where he navigates himself in different dates of the calendar. Nonetheless, a point comes(see the code) where the user action must send data to 2 get arrays-the one is directly affected by user action, the other indirectly(this is what i cannot achieve).I can do it to some extent but not in the way i want it exactly. Justsomeguy wrote this below: if($prev_day==0){$cmonth = $cmonth-1;}orif($prev_day==0){$cmonth--;} Despite it does not work in, the way of thinking is correct. If you are confused i am going to prepare a picture to see-a flowchart, to understand better.

Link to comment
Share on other sites

There aren't 2 $_GET arrays, there is 1. The only ways for a user to submit data to PHP are through $_GET, $_POST, and $_COOKIE (and $_FILES). If the data is transmitted through the URL, it appears in $_GET. If the data is in the body of the request, it appears in $_POST (or $_FILES). If the data is in a header, it appears in $_COOKIE. Those are the only ways to get user-submitted data to PHP. If your page needs to send any data to PHP then it does so through one of those methods. I think Ingolme and I are confused here because we don't know what problem you're actually trying to solve. This started with an abstract discussion about global variables, then scoping, then post 7 had a problem with an if statement, then post 9 wasn't decrementing a variable correctly, and now you're asking questions about things like $_GET, but that doesn't seem to be the problem you're actually trying to solve. This code works perfectly fine: if($prev_day==0){$cmonth = $cmonth-1;} it decrements the $cmonth variable by 1 if the condition is true. If that doesn't work for your situation then I'm not really sure what to say about that because I don't know what the problem is.

Link to comment
Share on other sites

The problem is you have to validate each day and month, and return a valid value for day and month if it falls below 0, and subtract a year if month falls below 0

<?php$prevc_month=1;$prev_day=0;$cprevyear=2012;$calc_date = mktime(0,0,0,$prevc_month, $prev_day, $cprevyear);if($prev_day<=0 && $prevc_month >1 ){$prevc_month--;$calc_date = mktime(0,0,0,$prevc_month, 1, $cprevyear);$prev_day= date('t', $calc_date ); // get total number of day of prev month and reset $prev_day$calc_date = mktime(0,0,0,$prevc_month, $prev_day, $cprevyear); //get new timestamp valueecho date("d-m-Y", $calc_date).'<br />IF day == 0 and month greater than 1<br />'; // echo out date}else if($prev_day<=0 && $prevc_month <=1 ){$prevc_month=12;$cprevyear--;$calc_date = mktime(0,0,0,$prevc_month, 1, $cprevyear);$prev_day= date('t', $calc_date ); // get total number of day of prev month and reset $prev_day$calc_date = mktime(0,0,0,$prevc_month, $prev_day, $cprevyear);//get new timestamp valueecho date("d-m-Y", $calc_date).'<br />IF day == 0 and month == 1<br />';// echo out date}else if($prev_day>0 && $prevc_month <=0 ){$prevc_month=12;$cprevyear--;$calc_date = mktime(0,0,0,$prevc_month, $prev_day, $cprevyear);//get new timestamp valueecho date("d-m-Y", $calc_date).'<br />IF day > 0 and month <=0<br />';// echo out date}else{echo date("d-m-Y", $calc_date).'Else<br />';// echo out date}echo '<a href="'.$_SERVER["PHP_SELF"]. "?day= ". $prev_day ."&monthcol=".$prevc_month."&yearcol=".$cprevyear.'">Previous day</a>';?>

Link to comment
Share on other sites

I am approaching a solution here-AT LAST, but i am still not completely done. I was considering posting more code but that may not be needed after all. The code that made a reduction in the month(-as i want from february to january ) is this:

if(isset($_GET["day"]))		{	   $cMonth--;		}

justsomeguy is very close to what you write above-in addition, sorry for the confusion in my posts. So, the month is reduced by one(cause of $cMonth-- ;) the moment i load the page, something i do not want, the reason this happens is cause of the following code at the beginning of the script.

if (!isset($_GET["day"])) $_GET["day"]  = date("j" );

Of course the above is needed since i want the day of the month going to today's day. The above code outputs the current day here:

echo <li>'.$cDay.'  '.$monthNames1[$cMonthcol- 1].'  '.$cYearcol.'</li>

So, as i see it, one of the 2 code segments(or both) listed above need some "fixing". I forgot this:

$cDay=$_GET["day"];

Thanks for the patience As i see now this also works:

  if($cDay==29)		{	   $cMonth--;		}  

I placed 29 above just for test

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...