kirk112 Posted August 20, 2007 Share Posted August 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65812-solved-references/ Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65812-solved-references/#findComment-328814 Share on other sites More sharing options...
kirk112 Posted August 20, 2007 Author Share Posted August 20, 2007 Arrr I now understand. I knew about isset but was trying to be lazy have about 50 values and did not want to do if isset() for each of them Thanks for the post!! Quote Link to comment https://forums.phpfreaks.com/topic/65812-solved-references/#findComment-328819 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.