前提・実現したいこと
現在Laravelを学習するため、青本(https://www.amazon.co.jp/PHP%E3%83%95%E3%83%AC%E3%83%BC%E3%83%A0%E3%83%AF%E3%83%BC%E3%82%AF-Laravel%E5%85%A5%E9%96%80-%E6%8E%8C%E7%94%B0%E6%B4%A5%E8%80%B6%E4%B9%83/dp/4798052582)に取り組んでいます。
2章のコントローラーの利用にて、複数のアクションの利用を学習中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
syntax error, unexpected 'global' (T_GLOBAL), expecting function (T_FUNCTION) or const (T_CONST)
該当のソースコード
PHP
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6 7class HelloController extends Controller 8{ 9 global $head, $style, $body, $end; 10 $head = '<html><head>'; 11 $style = <<<EOF 12 <style> 13 body {font-size:16px; color:#999;} 14 h1 {font-size:100px; text-align:right; color:#eee; margin:-40px 0 -50px 0;} 15 </style> 16 EOF; 17 $body = '</head><body>'; 18 $end = '</body></html>'; 19 20 function tag($tag, $txt) { 21 return "<{$tag}>".$txt."</{$tag}>"; 22 } 23 24 class HelloController extends Controller 25 { 26 public function index() { 27 global $head, $style, $body, $end; 28 29 $html = $head.tag('title', 'Hello/Index').$style.$body.tag('h1', 'Index').tag('p', 'this is Index page').'<a href="/hello/other">go to Other page</a>'.$end; 30 return $html; 31 } 32 33 public function other() { 34 global $head, $style, $body, $end; 35 36 $html = $head.tag('title', 'Hello/Other').$style.$body.tag('h1', 'Other').tag('p', 'this is Other page').'<a href="/hello/other">go to Other page</a>'.$end; 37 return $html; 38 } 39 40 } 41} 42
試したこと
Googleやteratailなどでエラーについて検索を試みました。
syntax errorが構文エラーであることは分かり、その後の情報が手がかりだといのは分かったのですが、青本を写経している段階なので何が問題なのかが全くわからないという状況です…。
補足情報(FW/ツールのバージョンなど)
PHP 7.3.6
Laravle 5.8
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/10 12:58
2019/06/10 13:10
2019/06/10 13:15
2019/06/10 13:19
2019/06/10 13:22