Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Fatal error: Only variables can be passed by reference

Posted on 2014-04-29
12
Medium Priority
?
908 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 111

Expert Comment

by:Ray Paseur
ID: 40031715
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
ID: 40031856
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 111

Expert Comment

by:Ray Paseur
ID: 40031880
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:Surrano
ID: 40032259
Try this:

    array_push($params, 4);

Open in new window

0
 
LVL 8

Expert Comment

by:Surrano
ID: 40032272
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
ID: 40033628
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
 
LVL 8

Expert Comment

by:Surrano
ID: 40033893
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 111

Expert Comment

by:Ray Paseur
ID: 40034284
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
ID: 40035995
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 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 40036039
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 1000 total points
ID: 40041526
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
ID: 40042538
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

972 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