php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #16857 short if assignment syntax for static vars
Submitted: 2002-04-26 12:09 UTC Modified: 2002-04-28 21:13 UTC
From: msopacua at idg dot nl Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.2.0 OS: Any
Private report: No CVE-ID: None
 [2002-04-26 12:09 UTC] msopacua at idg dot nl
Would be nice if this worked. But the short syntax isn't accepted for a static var, even it's not in the initial declaration.

<?php
function sql_num_rows($result)
{
    static $cmd='undefined';
    if($cmd == 'undefined')
    {
        $cmd=($this->pg_new_api) ? 'pg_num_rows' : 'pg_numrows';
    }
    return @$cmd($result);
}
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-28 20:03 UTC] sniper@php.net
Works fine for me. Can you please add a short and complete
example which shows clearly what you're exactly asking for?

--Jani

 [2002-04-28 21:13 UTC] msopacua at idg dot nl
I backtraced my sources and I seem to have combined the fact that arithmetic expressions can't be used on static vars, with the short if-statement.

Besides the code I submited, I also used:
<?php
function testint($row=-1)
{
        static $currentrow=-1;
        $currentrow=($row == -1) ? $currentrow++ : $row;
        echo "$currentrow\n";
}
?>

which didn't increment $currentrow, nor did it issue any warning.

When looking for info, I found the user-contributed message:
http://www.php.net/manual/en/language.variables.scope.php

By tomek@pluton.pl, at 10-Dec-2001:
static $var = 2+3; //any expression

which lead me to believe that also () ? : expressions wouldn' t work, but it's the actual assignment that is ignored.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 17:01:27 2024 UTC