Solved

Fatal error: Only variables can be passed by reference

Posted on 2014-04-29
12
833 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
12 Comments
 
LVL 110

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 110

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 110

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 110

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
PHP Curl Problem 10 76
Dump data from mysql to xls php 10 56
Add a date range function to PDF Generation 9 42
Tell me if I understand this correctly... 6 20
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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 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