PHP parse error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

PHP parse error

getting this PHP error. Any advice? ( ! ) Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in C:\wamp64\www\ContractorPHPQ1\get-method.php on line 15 PHP code <?php class Info { public $name; function __construct($name) { $this->name = $name; } function get_name() { return $this->name; } // Check if the form is submitted if ( isset( $_GET['submit'] ) ) { // retrieve the form data by using the element's name attributes value as key $firstname = $_GET['firstname']; $lastname = $_GET['lastname']; // display the results echo '<h3>Form GET Method</h3>'; echo 'Your name is ' . $firstname . ' ' . $lastname; exit; } } $object = new Info("Form Method"); echo $object->get_name(); ?> html code <form action="get-method.php" method="get"> <input type="text" name="firstname" placeholder="First Name" /> <input type="text" name="lastname" placeholder="Last Name" /> <input type="submit" name="submit" /> </form>

23rd Nov 2019, 8:30 PM
Making A Change
Making A Change - avatar
2 Answers
+ 1
PHP processor wasn't expecting an `if` statement at line 15. Looking at your code that part seems to be the `if` block that checks for form submission values. The problem is; you put the `if` block in your class, but not inside a valid block (e.g. a function). I recommend to move the `if` block out of your class block, either before, or after the line where you create <$object> and invoke its `get_name()` method. if ( isset( $_GET['submit'] ) ) { // some code here } $object = new Info ... // can be here or above the `if` block. Hth, cmiw
24th Nov 2019, 4:56 AM
Ipang
+ 1
Thanks i will try it
24th Nov 2019, 11:46 AM
Making A Change
Making A Change - avatar