Are there pointers in php?

后端 未结 9 1780
余生分开走
余生分开走 2020-11-28 04:06

What does this code mean? Is this how you declare a pointer in php?

$this->entryId = $entryId;
相关标签:
9条回答
  • 2020-11-28 04:28

    No, As others said, "There is no Pointer in PHP." and I add, there is nothing RAM_related in PHP.

    And also all answers are clear. But there were points being left out that I could not resist!

    There are number of things that acts similar to pointers

    • eval construct (my favorite and also dangerous)
    • $GLOBALS variable
    • Extra '$' sign Before Variables (Like prathk mentioned)
    • References

    First one

    At first I have to say that PHP is really powerful language, knowing there is a construct named "eval", so you can create your PHP code while running it! (really cool!)

    although there is the danger of PHP_Injection which is far more destructive that SQL_Injection. Beware!

    example:

    Code:

    $a='echo "Hello World.";';
    eval ($a);
    

    Output

    Hello World.

    So instead of using a pointer to act like another Variable, You Can Make A Variable From Scratch!


    Second one

    $GLOBAL variable is pretty useful, You can access all variables by using its keys.

    example:

    Code:

    $three="Hello";$variable=" Amazing ";$names="World";
    $arr = Array("three","variable","names");
    foreach($arr as $VariableName)
        echo $GLOBALS[$VariableName];
    

    Output

    Hello Amazing World

    Note: Other superglobals can do the same trick in smaller scales.


    Third one

    You can add as much as '$'s you want before a variable, If you know what you're doing.

    example:

    Code:

    $a="b";
    $b="c";
    $c="d";
    $d="e";
    $e="f";
    
    echo $a."-";
    echo $$a."-";   //Same as $b
    echo $$$a."-";  //Same as $$b or $c
    echo $$$$a."-"; //Same as $$$b or $$c or $d
    echo $$$$$a;    //Same as $$$$b or $$$c or $$d or $e
    

    Output

    b-c-d-e-f


    Last one

    Reference are so close to pointers, but you may want to check this link for more clarification.

    example 1:

    Code:

    $a="Hello";
    $b=&$a;
    $b="yello";
    echo $a;
    

    Output

    yello

    example 2:

    Code:

    function junk(&$tion)
    {$GLOBALS['a'] = &$tion;}
    $a="-Hello World<br>";
    $b="-To You As Well";
    echo $a;
    junk($b);
    echo $a;
    

    Output

    -Hello World

    -To You As Well

    Hope It Helps.

    0 讨论(0)
  • 2020-11-28 04:28

    To answer the second part of your question - there are no pointers in PHP.

    When working with objects, you generally pass by reference rather than by value - so in some ways this operates like a pointer, but is generally completely transparent.

    This does depend on the version of PHP you are using.

    0 讨论(0)
  • 2020-11-28 04:29

    entryId is an instance property of the current class ($this) And $entryId is a local variable

    0 讨论(0)
提交回复
热议问题