Jump to content


Photo

PHP operators in $_SESSIONS


  • Please log in to reply
2 replies to this topic

#1 empec

empec

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 13 July 2014 - 11:28 AM

i'm trying to develop a deeper understanding of sessions, and I found this article on php.net regarding sessions with the following script;

<?php
    // Get the private context
    session_name('Private');
    session_start();
    $private_id = session_id();
    $b = $_SESSION['pr_key'];
    session_write_close();
   
    // Get the global context
    session_name('Global');
    session_id('TEST');
    session_start();
   
    $a = $_SESSION['key'];
    session_write_close();

    // Work & modify the global & private context (be ware of changing the global context!) 
?> 
<html>
    <body>
        <h1>Test 2: Global Count is: <?=++$a?></h1>
        <h1>Test 2: Your Count is: <?=++$b?></h1>
        <h1>Private ID is <?=$private_id?></h1>
        <h1>Gloabl ID is <?=session_id()?></h1>
        <pre>
        <?php print_r($_SESSION); ?> 
        </pre>
    </body>
</html>
<?php
    // Store it back
    session_name('Private');
    session_id($private_id);
    session_start();
    $_SESSION['pr_key'] = $b;
    session_write_close();

    session_name('Global');
    session_id('TEST');
    session_start();
    $_SESSION['key']=$a;
    session_write_close(); 
?> 

[EDIT BY danbrown AT php DOT net: Contains a bugfix provided by (lveillette AT silexmultimedia DOT com) on 19-NOV-09.]

I do not understand line 21 and 22.

<h1>Test 2: Global Count is:<?=++$a?></h1>

If I change this to;

        <h1>Test 2: Global Count is:<?php echo =++$a; ?></h1>
or
        <h1>Test 2: Global Count is:<?php =++$a ?></h1>

I would get a parse error, unexpected "=" in line 21.

Can anyone explain why this is?

* Why is it being echoed
* Why can I not use echo in this case
* Why does the code not start with <?php ?
* What does line 21 even mean? =++$a?

 

 

 

Thanks in advance guys!


Edited by empec, 13 July 2014 - 11:30 AM.


#2 empec

empec

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 13 July 2014 - 11:39 AM

I found out that <?=++$a?> is just a shortcut for saying <?php echo ++$a; ?>

 

Thanks empec! ;)


Edited by empec, 13 July 2014 - 11:39 AM.


#3 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,539 posts

Posted 13 July 2014 - 12:56 PM

 

 

Can anyone explain why this is?

<?=   is shorthand syntax for <?php echo  

There is also another form of short tag which is  <?  this is shorthand syntax for the full open tag  <?php

 

When using short tag syntax be-careful as these are controlled by a PHP directive called  short_open_tag  . Not all server configurations has this directive enabled. If this directive is not enabled on a server then your PHP code will be completely ignored and will be output to the browser!

 

NOTE: As of PHP 5.4+  <?=  is always available regardless of the short_open_tag directive status. For older versions of PHP the above warning still applies!

 

I only recommend using the short tags in template files only. For all other cases always use the full php tags syntax.

 

As for  ++$a  it is shorthand syntax for  $a = $a + 1;


Edited by Ch0cu3r, 13 July 2014 - 01:00 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com