Jump to content

[SOLVED] References??


kirk112

Recommended Posts

I am trying to get my head around references and why you would use them,

 

for example if the $_get['page'] is not passed to the page I get 'Notice: Undefined index: per_page in ' but if I put the & before I get no error messages, is this one of the usages of refeneces?

 

$array = array(

'per_page'              => &$_GET['per_page'],

'per_page_extra'      => $_GET['per_page'])

 

Sorry for the simple question

 

thanks for your help

Link to comment
Share on other sites

erm.. no

 

thats what isset() is for

 

ie

if( isset($_GET['page']) )
{
echo $_GET['page'];
}

 

as for referances

instead of passing a value to a function and then having to return it you can use a referance

ie

Note: the foo function is NOT returning anything, but $a still increases by 1

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
echo $a."<br>";
foo($a);
echo $a."<br>";
?> 

 

 

What References Are

 

References in PHP are a means to access the same variable content by different names. They are not like C pointers; instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The most close analogy is with Unix filenames and files - variable names are directory entries, while variable contents is the file itself. References can be thought of as hardlinking in Unix filesystem.

 

SO.. to sum up

 

$b=100;
$a = $b;//$a equals 100
$c = & $b; //$c equals $b (not 100 but whatever $b is)

$b=$b+100;
echo "a=$a, b=$b, c=$c";

 

 

Hope that helps

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.