php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79647 Conflict between $GLOBALS["now"] and $now
Submitted: 2020-05-28 12:55 UTC Modified: 2020-05-29 08:15 UTC
From: murray at focus-computing dot com dot au Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.3.18 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: murray at focus-computing dot com dot au
New email:
PHP Version: OS:

 

 [2020-05-28 12:55 UTC] murray at focus-computing dot com dot au
Description:
------------
A value is assigned to $GLOBALS["now"], however this is removed when a value is assigned to $now, as in the example below

Test script:
---------------
$GLOBALS["now"] = time();

print "Year is " . strftime("%Y", $GLOBALS["now"]) . "\n";

$now = new DateTime("now");

print "Year is " . strftime("%Y", $GLOBALS["now"]) . "\n";


Expected result:
----------------
Year is 2020
Year is 2020


Actual result:
--------------
Year is 2020
<br /> <b>Warning</b>: strftime() expects parameter 2 to be int, object given in <b>[...][...]</b> on line <b>10</b><br /> Year is


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-05-28 13:08 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-05-28 13:08 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See <https://www.php.net/manual/en/reserved.variables.globals.php>.
 [2020-05-28 13:45 UTC] bugreports at gmail dot com
$GLOBALS["now"] and $now are the same variable in the global scope and there is no point to write $GLOBALS at all outside functions

<?php
$GLOBALS["now"] = time();
print "Year is " . strftime("%Y", $GLOBALS["now"]) . "\n";
$now = new DateTime("now");
print "Year is " . strftime("%Y", $GLOBALS["now"]) . "\n";
?>

versus

<?php
test();
function test()
{
 $GLOBALS["now"] = time();
 print "Year is " . strftime("%Y", $GLOBALS["now"]) . "\n";
 $now = new DateTime("now");
 print "Year is " . strftime("%Y", $GLOBALS["now"]) . "\n";
}
?>
 [2020-05-28 23:22 UTC] murray at focus-computing dot com dot au
I've been programming with PHP for about 15 years now.  I've always assumed the $GLOBALS was a reference to an array of variables unrelated to the variables defined outside of functions and classes.

I would also like to propose that these should be treated as separate.  The variable defined outside of a function or class is simply a local variable outside of a function or class.
 [2020-05-28 23:49 UTC] requinix@php.net
> I would also like to propose that these should be treated as separate.  The
> variable defined outside of a function or class is simply a local variable
> outside of a function or class.
I have a counter-proposal for you:

Don't use $GLOBALS.
 [2020-05-29 07:54 UTC] bugreports at gmail dot com
> I've been programming with PHP for about 15 years now. I've always 
> assumed the $GLOBALS was a reference to an array of variables 
> unrelated to the variables defined outside of functions and classes.

it's heroic to state that one did not understand lanuage basics withn 15 years of usage and even proposes to change basic concepts

https://www.php.net/manual/en/reserved.variables.globals.php
https://www.php.net/manual/en/language.variables.scope.php

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

> The variable defined outside of a function or class is simply 
> a local variable outside of a function or class

nonsense - you had 15 years time to learn some basics like https://www.php.net/manual/en/language.variables.scope.php

frankly if you write $GLOBALĂ–S['foo'] or global $foo withina function is the same and when you write $GLOBALS in the global scope you waste performance for no good reason (useless )

php > echo $GLOBALS['x'];
filename:       php shell code
function name:  (null)
number of ops:  4
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   1     0  E >   FETCH_R                      global              ~0      'GLOBALS'
         1        FETCH_DIM_R                                      ~1      ~0, 'x'
         2        ECHO                                                     ~1
   2     3      > RETURN                                                   null

branch: #  0; line:     1-    2; sop:     0; eop:     3; out0:  -2
path #1: 0,
php > echo $x;
filename:       php shell code
function name:  (null)
number of ops:  2
compiled vars:  !0 = $x
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   1     0  E >   ECHO                                                     !0
   2     1      > RETURN                                                   null

branch: #  0; line:     1-    2; sop:     0; eop:     1; out0:  -2
path #1: 0,
php >
 [2020-05-29 08:05 UTC] bugreports at gmail dot com
> The variable defined outside of a function or class is simply 
> a local variable outside of a function or class

to make it clear - it even is - but yoz have to replace "local" with "scope" and $GLOBALS is to access variables in the global scope from functions/methods

it's all about scopes
 [2020-05-29 08:15 UTC] cmb@php.net
Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 18:01:29 2024 UTC