Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I've been working on this code error for my worpress site offline.

Parse error: syntax error, unexpected '[' in C:\Users\guyzer\Desktop\InstantWP_4.3\iwpserver\htdocs\wordpress\wp-content\themes\thesis_182\custom\custom_functions.php on line 169

Line code error line 169: $post_date = explode(" ", $post_event)[0];

On the live site, site is working but when i duplicate the live site using duplicator and transfer to offline or other servers this error always occurs which turn the site down. I hope you can help me solve this error!

Here is the whole code for the error:

    <div class="page">

        <div class="tab-sidebars">
            <h1><a href="<?php echo get_site_url()?>/gigs">GIGS</a></h1>
            <h3><a href="<?php echo get_site_url()?>/gigs/today">&#8226; Today</a></h3>
            <h3><a href="<?php echo get_site_url()?>/gigs/weeks">&#8226; Weeks</a></h3>
            <h3><a href="<?php echo get_site_url()?>/gigs/month">&#8226; Month</a></h3>
        </div>

        <div id="gigs-carousel" class="post-container">

            <a class="buttons prev" href="#"></a>

            <div class="viewport">
                <ul class="overview" style="width:9999px !important">
                <?php
                global $post;
                $today = getdate();
                $args = array(  'category_name' => 'gigs', 
                                'post_type' => 'post', 
                                'meta_key' => custom_event_date, 
                                'orderby' => meta_value, 
                                'order' => 'ASC', 
                                'showposts' => '99'
                            );

                $the_query = new WP_Query($args);
                while($the_query->have_posts()) : 
                    $the_query->the_post();
                    $post_id = $post->ID;
                    $post_event = get_post_meta($post_id, 'custom_event_date', true);
                    $post_date = explode(" ", $post_event)[0];
                    $post_time = explode(" ", $post_event)[1];
                    $post_day = explode("-", $post_date)[2];
                    $post_permalink = get_permalink($post_id);
                    $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'thumbnail' );

                    $date_now = date("Y-m-d H:i");
                    $date_compare = $post_event;
                    $date_result =  strtotime($date_compare) - strtotime($date_now);

                    $current_month = date("m");
                    $event_month = explode("-", $post_date)[1];

                    if($date_result > 0 && $current_month == $event_month) :
                ?>
                    <li style="background-image:url(<?php echo $thumbnail_src[0]?>)">
                        <div class='post-day'><?php echo $post_day?></div>
                        <a class='post-item' href="<?php echo $post_permalink?>" >
                            <div class='post-title'><?php echo get_the_title();?></div>
                            <div class='post-sub'><?php echo convert_time($post_time)?></div>
                            <div class='post-excerpt'><?php the_excerpt(); ?></div>
                        </a>
                    </li>

                <?php
                    endif;
                endwhile;
                wp_reset_postdata();
                ?>
                </ul>
            </div><!-- .viewport -->


            <a class="buttons next" href="#"></a>

        </div><!-- #gigs-carousel -->

    </div>
share|improve this question
3  
That sort of syntax is only available in PHP 5.4. –  aynber Oct 14 '13 at 18:56
    
You probably want to upgrade or use $post_date = explode(" ", $post_event); $post_date = $post_date[0];. –  Dave Chen Oct 14 '13 at 18:58
1  
Lesson to be learned: Make sure you know what PHP version you're running, and make sure you have the same versions for your dev and live sites. If it works on your live site but not dev, then it sounds like you need to upgrade your dev server to the more recent PHP version and make it the same as your live server. –  Spudley Oct 14 '13 at 19:10
    
Hello guys here is the whole code for the site. [guyzer.net/problem-code.txt][1] [1]: guyzer.net/problem-code.txt hope to fix this problem. thanks for all your quick response im trying all your suggestions. right now. regards, brian –  Brian Jake Tolosa Oct 14 '13 at 19:12
add comment

marked as duplicate by John Conde, andrewsi, Chris, jww, jkschneider Feb 17 at 1:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

$post_date = explode(" ", $post_event)[0];

You're probably trying to use array dereferencing feature on a PHP version that doesn't support it. It's available only on PHP 5.4+ versions.

From the PHP manual:

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

As it says, you'll have to use a temporary variable on older versions of PHP:

$temp = explode(" ", $post_event);
$post_date = $temp[0];

Change all the occurences similarly.

Or, you could use list() to do it in one line (reduces readability a bit, though):

That is, you can replace:

$post_date = explode(" ", $post_event)[0];
$post_time = explode(" ", $post_event)[1];

with this:

list($post_date, $post_time) = explode(" ", $post_event);

However, using a temporary variable and manually assigning the values is more neater and readable.

share|improve this answer
    
Hello guys here is the whole code for the site. [guyzer.net/problem-code.txt][1] [1]: guyzer.net/problem-code.txt hope to fix this problem. thanks for all your quick response im trying all your suggestions. right now. regards, brian – Brian Jake Tolosa –  Brian Jake Tolosa Oct 14 '13 at 19:19
    
@BrianJakeTolosa: What exactly is your question? –  Amal Murali Oct 14 '13 at 19:34
    
here is the working site duplicated guyzer.net/prc when i remove the guyzer.net/problem-code.txt code site works fine. but when i bring back the code from problem-code.txt site is not working and having the line 169 error. the problem-code should work but when im transfering the site to another server using duplicator it shows the 169 error and i cannot fix it. –  Brian Jake Tolosa Oct 14 '13 at 19:46
    
this is the whole code. guyzer.net/problem-code.txt this should be working. if you have spare time to fix it and work on the problem guyzer.net/prc i can give you access. –  Brian Jake Tolosa Oct 14 '13 at 19:58
add comment

From the PHP docs:

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

You are most likely using PHP 5.3 on the machine getting the error.

share|improve this answer
add comment

I believe you need PHP >= 5.4 for that:

explode(" ", $post_event)[0]

Try:

list($post_date, $post_time)   = explode(" ", $post_event);
list($junk, $junk2, $post_day) = explode("-", $post_date);
share|improve this answer
    
There's no need to use $junk -- you can do list(,,$post_day) = explode('-', $post_date); –  Amal Murali Oct 14 '13 at 19:18
    
so the code should look like this?:-----> $post_event = get_post_meta($post_id, 'custom_event_date', true); list($post_date, $post_time) = explode(" ", $post_event); $post_day = explode("-", $post_date)[2]; –  Brian Jake Tolosa Oct 14 '13 at 19:35
    
@Brian Jake Tolosa: No, just what I put replaces your 3 problem lines that end in [n]. –  AbraCadaver Oct 14 '13 at 19:41
    
@Amal Murali: Agreed, however if you need those vars later, better to save them. –  AbraCadaver Oct 14 '13 at 19:41
    
@AbraCadaver i replace the line 169 ($post_date = explode(" ", $post_event)[0];) with: list($post_date, $post_time) = explode(" ", $post_event); list($junk, $junk2, $post_day) = explode("-", $post_date); but it wont work?? –  Brian Jake Tolosa Oct 14 '13 at 20:11
show 1 more comment

Just explode $post_event into it's own array and then act on that.

$post_event = get_post_meta($post_id, 'custom_event_date', true);
$post_dates = explode(" ", $post_event); 
$post_date = $post_dates[0];
$post_time = $post_dates[1];
$post_day = $post_dates[2];
share|improve this answer
    
hello T0w3ntuM, i did this but another error replace error line 169 into this error: Parse error: syntax error, (unexpected T_VARIABLE in line 170) which is line 170 is: the ($post_date = $post_dates[0];) –  Brian Jake Tolosa Oct 14 '13 at 20:15
    
because my code is missing a semi-colon on line 169. –  T0w3ntuM Oct 15 '13 at 14:30
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.