Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Parse error: syntax error, unexpected T_PUBLIC [closed]

Tags:

php

I am getting this error in this PHP code on line 3, what could be wrong? This code has been taken from php manual user notes by frank at interactinet dot com

<?php

public function myMethod()
{
return 'test';
}

public function myOtherMethod()
{
return null;
}

if($val = $this->myMethod())
{
 // $val might be 1 instead of the expected 'test'
}

if( ($val = $this->myMethod()) )
{
// now $val should be 'test'
}

// or to check for false
if( !($val = $this->myMethod()) )
{
// this will not run since $val = 'test' and equates to true
}

// this is an easy way to assign default value only if a value is not returned:

if( !($val = $this->myOtherMethod()) )
{
$val = 'default'
}

?> 
like image 387
Ashish Yadav Avatar asked Nov 12 '12 09:11

Ashish Yadav


1 Answers

The public keyword is used only when declaring a class method.

Since you're declaring a simple function and not a class you need to remove public from your code.

like image 185
DiverseAndRemote.com Avatar answered Oct 14 '22 21:10

DiverseAndRemote.com