Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Fatal error: Only variables can be passed by reference

Posted on 2014-04-29
12
Medium Priority
?
881 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 look for a specific file type in a local or remote server directory using PHP.

670 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