Solved

Fatal error: Only variables can be passed by reference

Posted on 2014-04-29
12
748 Views
Last Modified: 2014-05-05
<?php
function readParams( $source ) {
    $params = array();
    $fh = fopen( $source, 'r' ) or die("problem with $source");
    while ( ! feof( $fh ) ) {
        $line = trim( fgets( $fh ) );
        if ( ! preg_match( "/:/", $line ) ) {
            continue;
        }
        list( $key, $val ) = explode( ':', $line );
        if ( ! empty( $key ) ) {
            $params[$key]=$val;
        }
    }
    fclose( $fh );
    //$params.='extra';
    $params=array_push(4);
    return $params;
}

function writeParams( $params, $source ) {
    $fh = fopen( $source, 'w' ) or die("problem with $source");
    foreach ( $params as $key=>$val ) {
        fputs( $fh, "$key:$val\n" );
    }
    fclose( $fh );
}


$file = "./params.txt"; 
$array['key1'] = "val1j";
$array['key2'] = "val2j";
$array['key3'] = "val3j";
writeParams( $array, $file );
$output = readParams( $file );
print_r( $output ); 


?>

Open in new window



in order to learn (I think)
I am trying to add to the $params variable

I tried adding a string but there was array to string error

so now I am trying to push a value to the end of array


Fatal error: Only variables can be passed by reference in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_06_Code\listing06.01.php on line 17

and what does this error mean
what is difference pass by value, pass by reference
0
Comment
Question by:rgb192
  • 4
  • 4
  • 4
12 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
On line 17, the number 4 is not a variable, hence the fatal error.

On any occasion that you encounter a function that you're not 100% sure you understand, you should look up the function definition on PHP.net.  Example:
http://php.net/manual/en/function.array-push.php

If you read the man page and still do not understand how to write the statement on line 17, please post back and I'll be glad to help.

If you want to learn the difference between variables and references, this article explains it.  But I think the misunderstanding is really about how (or whether) to call array_push().
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
0
 

Author Comment

by:rgb192
Comment Utility
not only did I read
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html

but I also ran all your code samples in my ide


but I do not understand the php error (how did I pass by reference???)

$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);


//$params = array("orange", "banana");
array_push($params, "apple", "raspberry");
//print_r($stack);


Array ( [key1] => val1j [key2] => val2j [key3] => val3j [0] => apple [1] => raspberry )

this does not add to the keys
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I think the error occurred on this line, according to the line number in the error message:

$params=array_push(4);

That does not make sense because the function expects array &$array for the first argument of the function.
0
 
LVL 8

Expert Comment

by:Surrano
Comment Utility
Try this:

    array_push($params, 4);

Open in new window

0
 
LVL 8

Expert Comment

by:Surrano
Comment Utility
in the fruity example, do you expect that new elements are created with keys "key4" and "key5"? Well, that won't work. What array_push does is to take the lowest non-negative integer that doesn't exist in the array yet, and uses it as a key. You'll have to implement your own function to achieve some custom numbering.
0
 

Author Comment

by:rgb192
Comment Utility
Array ( [key1] => val1j [key2] => val2j [key3] => val3j [0] => 4 )

array_push($params, 4);
added
key 0, value 4

 lowest non-negative integer that doesn't exist in the array yet, and uses it as a key
so key=0



That does not make sense because the function expects array &$array for the first argument of the function.
variable is $array
reference is &$array

I do not understand a practical example
array_push($params, 4);

$four=4;
array_push($params, &$four);
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Expert Comment

by:Surrano
Comment Utility
No, this ref thing is in the definition of the array_push function. When you call it, you don't use the "&" sign.  Also, only the first parameter (the array) is what must be a reference.

To understand reference: In C analogy, a left-value, i.e. whatever may appear on the left side of an assignment operator. This makes sense:
$four=3+1;

This doesn't:
4=3+1;

Since array_push wants to modify the array you specify as the first parameter, you may invoke it like this:

array_push($myarray, 1);

but you can't use a literal:

array_push(array(11,22,33), 44);

Okay, I could imagine it to run without errors, but makes no sense and can't test it right now.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Let's not overcomplicate...

In the original code snippet, we found a fatal error on line 17 where it said this:

$params=array_push(4);

Please step back from the technical details and just tell us in plain language what you want to accomplish when you wrote that instruction.  For example, "I wanted to put the number 4 into the first (or last) position of the $params array."  Or "I wanted to add 4 to one of the values in the $params array."  Or whatever explains why you wrote the instruction.  Once we understand that, we can show you the correct PHP statement to get it done.

Thanks, ~Ray
0
 

Author Comment

by:rgb192
Comment Utility
I am getting
Array ( [key1] => val1j [key2] => val2j [key3] => val3j [0] => 4 )

but output I would like is
Array ( [key1] => val1j [key2] => val2j [key3] => val3j [key4] => 'secret' )

When you call it, you don't use the "&" sign.  Also, only the first parameter (the array) is what must be a reference.
so the array is the reference and the second parameter is the value which is the value to be added to array

array_push($myarray, 1);
$myarray is reference
1 is value


is this correct?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
You would get the desired result with an assignment statement, something like this:

$myarray['key4'] = 'secret';

Open in new window

0
 
LVL 8

Assisted Solution

by:Surrano
Surrano earned 250 total points
Comment Utility
The catch here is that you probably want to generate the key.

How about this?

$myarray['key'.(count($myarray)+1)] = 'secret';

Open in new window


But I still feel like the real question being, why do you want to enumerate the keys with anything other than 0..(n-1)? Are these keys coming from somewhere? (e.g. from a database query) In that case you should get the next key from an iteration (e.g. fetch row #n from database query above)
0
 

Author Closing Comment

by:rgb192
Comment Utility
okay, this can not be done with push

Are these keys coming from somewhere?
no this is a book php code example

thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now