Solved

Fatal error: Only variables can be passed by reference

Posted on 2014-04-29
12
806 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 109

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 109

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 109

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 109

Accepted Solution

by:
Ray Paseur earned 250 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 250 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

777 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