1

I am loading a friends site to play with it and it gives me this: Notice: Array to string conversion error. Any idea's how to fix this? (my friend is out of town)

function href($filename) {
    if (@$_SERVER['HTTPS'] != 'on') {
        return HTTP_IMAGE . $filename;
    } else {
        return HTTPS_IMAGE . $filename;
    }   
}

the error points to the 3rd line on this code.

4
  • 1
    You must be passing an array instead of a string as $filename. Commented Feb 11, 2014 at 17:12
  • 2
    what is the $filename value? Commented Feb 11, 2014 at 17:13
  • What is $filename? It's clearly not a string. How are you calling href()? Commented Feb 11, 2014 at 17:19
  • 1
    A method you should know about... isset. It and var_dump will be very useful. Suppressing, @,is rarely ideal Commented Feb 11, 2014 at 17:21

2 Answers 2

2

You're mixing array and string in a return. Try the following:

  • specify an index to $filename (like $filename[0])

or:

  • convert it into a string with for example implode() function (like implode(',', $filename);, which gives you an string with the values of the array $filename, segregated by ,).
Sign up to request clarification or add additional context in comments.

3 Comments

How do you know what $filename contains? =P
@crush well I guess it's an array due to the error. I don't think more information is necessary to provide a solution in this case.
and there's always var_dump to prove what @DanielLisik mentioned
1

You are using a string concatenation operator with something that is not a string. Namely, with an array.

Try this:

function href($filename) {
var_dump($HTTP_IMAGE);
var_dump($filename);
    if (@$_SERVER['HTTPS'] != 'on') {
        return HTTP_IMAGE . $filename;
    } else {
        return HTTPS_IMAGE . $filename;
    }   
}

When you run this code it should display whatever is inside HTTPS_IMAGE and $filename.

As I do not know what they contain, I will explain with the following sample code:

$my_array[0] = 'Hello';
$my_array[1] = 'world';
$my_array[2] = '!';
var_dump( $my_array );

If you execute that code you will obtain the following:

array(3) {
  [0]=>
  string(5) "Hello"
  [1]=>
  string(5) "world"
  [2]=>
  string(1) "!"
}

That is an array. It is a collection of elements, which need to be accessed by index. For instance, to access "world" you need to access it by its index, "1":

echo $my_array[1];

Returning to your example, identify which variable is an array and access it by the appropriate index. You will know which index is by inspecting the result of var_dump().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.