Jump to content

Help explaining PHP syntax


davidf85

Recommended Posts

I am have some trouble grasping the basics of PHP.

One thing in particular is how the session_start() function, can operate throughout every php page,

but variables (at least I believe this to be so) are particular to each script.

For example...

 

example.php

 

<?php

session_start()

?>

 

 

<?php

$a="b";

?>

 

<?php

$a='c';

?>

 

Does this just mean the SESSION function is an overlaying feature of PHP, that operates throughout the page regardless of what scripts are made and what variables are set?

 

Would

 

<?php

$a="b";

$_SESSION['a']=$a

?>

 

<?php

$a='c';

$_SESSION['a']=$a

?>

 

ultimately result as $_SESSION['a']= c ???

 

 

Link to comment
Share on other sites

The session_start() function allows you to store some information in the $_SESSION global so you can access it from another file without having to send the parameter in the query string or via post.

Regular variables that you declare inside your script are available trough that script only.

For example, lets say that this file is called example.php

<?php
$a = 10;
$b = 20;
?>

The variables $a and $b will be available for the time the script is running, after that you cannot access them from another file, only if you do

<?php
session_start();

$_SESSION['a'] = 10;
$_SESSION['b'] = 20;
?>

if that is example.php then lets say you have another file example1.php, you could

<?php
session_start();

echo $_SESSION['a'];
echo $_SESSION['b'];

?>

 

Just remember that session variables are design to store small amount of information

Link to comment
Share on other sites

The way sessions work is PHP gives the user a session cookie, and stores that particular sessions data on the server. Every time a page is loaded, (assuming you used session start) the browser sends this session cookie, and the server uses that value to determine what information that user has stored in session data. That data is used to populate the $_SESSION super global array. You will realize this after you clear your cookies, and then get logged off of each site you were previously logged into!

 

Variables on the other hand have a maximum scope of global to the script. They can't "travel" to other scripts the same way $_SESSION values seem to. However, when you include a page, variables from the page that is included will be available in the page that included it after the include statement. so for example

 

#page1
$v = "a";

#page2

include "page1.php";
echo $v;//echos: a

 

 

 

Does this just mean the SESSION function is an overlaying feature of PHP, that operates throughout the page regardless of what scripts are made and what variables are set?

yes, assuming you  use session_start to grab the relevant session data.

 

Would

 

<?php

$a="b";

$_SESSION['a']=$a

?>

 

<?php

$a='c';

$_SESSION['a']=$a

?>

 

ultimately result as $_SESSION['a']= c ???

 

 

yes.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.