Cannot use a scalar value as an array

I am a beginner!

I have copied the following code exactly form a book. When I try and run it I get the following error:
Warning: Cannot use a scalar value as an array in C:\wamp\www\Netbeans\formclass.php on line 35

What is a scalar value and why can I not use it in an array


<?php class formclass { var $fields = array(); var $processor; var $submit = "Submit form"; var $Nfields = 0; function __construct($processor, $submit) { $this->processor = $processor; $this->submit = $submit; } function displayform() { echo ""; echo ""; for($j=1; $j<=sizeof($this->fields);$j++) { echo "\n"; echo "\n"; echo ">\n"; echo "
"; } } function addField($name, $label) { $this->fields[$this->Nfields]['name'] = $name; $this->fields[$this->Nfields]['label'] = $label; $this->fields = $this->Nfields+ 1; } } ?>


and here is the code to display the form:

include_once (“formclass.php”);
echo “Create a form”;
$phone_form = new formclass(“process.php”, “Submit form”);
$phone_form->addField(“first_name”, “First name”);
$phone_form->addField(“last_name”, “Last name”);
$phone_form->addField(“phone”, “Phone”);
echo “

Please fill out the form

echo “”;
Any help to learn more is greatly appreciated. I thank you in advance

Is this all of formclass.php? There are not even 35 lines there

Like I said I copied everything from a book and that is all there is so I will have to say yes that is all of formclass.php

Your problem is most likely here:

[php]$this->fields = $this->Nfields+ 1;[/php]

I’m guessing this is meant to increment the array key so it should be

[php]$this->Nfields = $this->Nfields + 1;[/php]

Thank you M@tt that solved it

Sponsor our Newsletter | Privacy Policy | Terms of Service