Jump to content

Syntax errors


Xtremer360

Recommended Posts

Any ideas on how to fix this line with syntax errors present?

 

<?php $image_properties=array( 'src'=> if ( ! empty ($this->session->userdata('avatar') ) ) { 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar').'; } else { 'assets/peach/img/sprites/userinfo/avatars/avatar.png'; } , 'height' => '80px', 'width' => '80px', 'alt' => 'Avatar' ); ?>

Link to comment
https://forums.phpfreaks.com/topic/264534-syntax-errors/
Share on other sites

Yeah, it's called debugging and you don't do it by sticking the whole thing on one line.

 

Split everything up as follows:

 

<?php 

$image_properties=array(
     'src'=> 
          if(!empty ($this->session->userdata('avatar') ) ) {
                'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar').';
           } else {
                'assets/peach/img/sprites/userinfo/avatars/avatar.png';
           }, 
     'height' => '80px',
     'width' => '80px',
     'alt' => 'Avatar' 
); 

?>

 

Having spaced it out I can see you have a wack-off if statement in the middle of your bloody array. Not allowed, you've got to use an inline-if statement:

 

<?php 

$image_properties=array(
     'src'=> (!empty ($this->session->userdata('avatar') ) ) 
          : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
          ? 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),
     'height' => '80px',
     'width' => '80px',
     'alt' => 'Avatar' 
); 

?>

<?php

 

$image_properties=array(

    'src'=> (!empty ($this->session->userdata('avatar') ) )

          ? 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar')

          : 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),

    'height' => '80px',

    'width' => '80px',

    'alt' => 'Avatar'

);

 

?>

 

Sorry I put the colon and question mark the wrong way round. You also had an addition concatenation symbol and single apostrophe somewhere around the previous if statement; that's now been taken out.

Then you're doing something wrong.

 

<?php

$check = TRUE;

$array = array(
'key' => ($check ? 'foo' : 'bar'),
'cas' => 'bah'
);

print_r($array);

?>

 

returns

 

Array
(
    [key] => foo
    [cas] => bah
)

 

It's a PEBCAK issue. You haven't even told us what error you're getting, nor the code you're trying. Best of luck.

I'm still trying to figure out the syntax error in this code just like my post has said:

 

$image_properties = array(
                        'src' => (!empty ($this->session->userdata('avatar')                   
                        : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
                        ? 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' 
                    ); 
                ?>	

Okay so now there's an issue with : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar')

 

<?php 
                    $image_properties = array(
                        'src' => (!empty ($this->session->userdata('avatar')                   
                        : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
                        ? 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' )
                    ); 

No, there isn't.

 

You've thrown a closing bracket in the wrong place. You're more than welcome to keep guessing the solution, but I think I'm through with saying you've guessed wrong. Best of luck, my suggestion is to hire a programmer if you want the job done, or go back to the basics if you actually want to learn.

If you're having that much trouble with this one, try formatting it more simply at first. This is a complex line of code the way you have it and you aren't able to spot the error.

 

Here's your code made more simple IMO. I left in your error, and moved the ternary stuff out of the array and into an if. Do you see the error now?

 

<?php 
$src = 'assets/peach/img/sprites/userinfo/avatars/avatar.png';
if(!empty($this->session->userdata('avatar'))){
    $src = 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar');
}

$image_properties = array(
   'src' => $src,
   'height' => '80px',
   'width' => '80px',
   'alt' => 'Avatar' )
); 

I tried this but got a "cannot use method return value" error on that src line:

 

$image_properties = array(
                       'src' => (!empty ($this->session->userdata('avatar') ) )        
                        ? 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
                        : 'assets/peach/img/sprites/userinfo/avatars/avatar.png')  
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' 
                    );  

Well that's an entirely new error, and you should be able to compare the old one to see the original error.

 

empty() works on variables, and you're using a method which probably returns nothing if it's empty. So there's no variable.

Well that's an entirely new error, and you should be able to compare the old one to see the original error.

 

empty() works on variables, and you're using a method which probably returns nothing if it's empty. So there's no variable.

 

Or he's not counting the parentheses

Topic solved!

 

Solution:

<?php 
                    $avatar = $this->session->userdata('avatar');
                    $image_properties = array(
                       'src' => (!empty ($avatar) )        
                        ? 'assets/peach/img/sprites/userinfo/avatars/'.$this->session->userdata('avatar') 
                        : 'assets/peach/img/sprites/userinfo/avatars/avatar.png',
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' 
                    );  
                    ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.