N
N
Name Imechko2015-10-12 19:05:38
PHP
Name Imechko, 2015-10-12 19:05:38

An error in the code. Loser level?

I get this error:
Parse error: syntax error, unexpected T_DOUBLE_ARROW, expecting ']' in Z:\home\zion.ru\www\function\articles.php on line 4
Here is the line itself:
$art1 ["id"= >1, "title"=>"Title1", "date"=>"12-10-2015", "content"=>"Content1"];
What is the problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Night, 2015-10-12
@Elem3nt071

$art1 = ["id"=>1, "title"=>"Title1", "date"=>"12-10-2015", "content"=>"Content1"];

A
Alexey Skobkin, 2015-10-12
@skobkin

function articles_all() {
    $art1 = ["id"=>1, "title"=>"Title1", "date"=>"12-10-2015", "content"=>"Content1"];
    $art2 = ["id"=>2, "title"=>"Title2", "date"=>"13-10-2015", "content"=>"Content2"];
    
    $arr[0] = $art1;
    $arr[1] = $art2;
    
    return $arr;
}

Transforms, for example, into
// PHP 5.4+
function articles_all() {
    return [
        ['id' => 1, 'title' => 'Title1', 'date' => '12-10-2015', 'content' => 'Content1'],
        ['id' => 2, 'title' => 'Title2', 'date' => '13-10-2015', 'content' => 'Content2']
    ];
}

M
murrometz, 2015-10-12
@murrometz

So arrays in php do not need to be initialized this way.
The square quotes [] refer to a specific array value.
echo $art1["id"];

N
Name Imechko, 2015-10-12
@Elem3nt071

I put the = sign and still the error is on line 4.

<?php

function articles_all() {
    $art1 = ["id"=>1, "title"=>"Title1", "date"=>"12-10-2015", "content"=>"Content1"];
    $art2 = ["id"=>2, "title"=>"Title2", "date"=>"13-10-2015", "content"=>"Content2"];
    
    $arr[0] = $art1;
    $arr[1] = $art2;
    
    return $arr;
}

function articles_get($id) {
    return ["id"=>1, "title"=>"Это простой заголовок", "date"=>"12-10-2015", "content"=>"Здесь будет статья"];
}

function articles_new($title, $date, $content) {
    //Создание статьи
}

function articles_edit($id, $title, $date, $content) {
    //Редактирование статьи
}

function articles_delete($id) {
    //Удаление статьи
}

?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question