Strange call to a member function on a non-object php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
So I create a Supermarket class and initiate two objects like this:
$items[0] = new Supermarket("Item1", 2.70);
$items[1] = new Supermarket("Item2", 1.0);
Then I call the showItem() method on the first object of the array and it works:
$items[0]->showItem();
But when I try to use a for or a foreach loop through the array to show all the items I get the non-object error. The following won't work either:
$i = 0;
$items[i]->showItem();
Any ideas?

You are not using the variable sign $
$i = 0;
$items[$i]->showItem();

change like
$i = 0;
$items[i]->showItem();
$i = 0;
$items[$i]->showItem();

$items[$i] instead of $items[i]

Related

PHP - variable in constructor is empty in other functions of the class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 months ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to access an associative array $foo from a function inside the class. When I log the contents from another function it is empty. I am really unsure what I am doing wrong.
class Item {
function __construct($x = 1) {
$y = do_something($x);
$foo = [
'id' => $y['anotherID'],
'name' => $y['name']
];
}
function insertData($data) {
$variable = $this->foo['id'];
// if I print $this->foo['id'] I get no output
}
}
I have also tried another recommendation of using self::$foo but got all sorts of errors about private and static?
You need to use $this->foo = [] instead of $foo = []. It makes it a property of the class.

Dynamic Matrices in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have written the PHP code below in order to create a dynamic matrix with different count of columns per row but PHPStorm says the row variable is not defined. please help.
class reply
{
public $text;
private $row = array(array());
private $rowIndex = 0;
private $colIndex = 0;
public function Add($menu)
{
$this->$row[$this->rowIndex][$this->colIndex] = $menu;
$this->colIndex++;
}
public function NextRow()
{
$this->rowIndex++;
$this->colIndex = 0;
}
}
$this->$row is incorrect, it should be $this->row.

PHP console does not echo [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Okay this really strange or I am missing something. When I run this very simmple PHP script on cmd I get the expected output which is 0. but when I uncomment the last two lines of code. . .nothing is displayed.
<?php
$test1 = 0;
echo $test1;
$test2 = 0;
#$test1_weight = 0:
#$test2_weight = 0;
?>
Is there some rule against declaring variables after an echo statement?
$test1_weight = 0:
--> change ":" to ";"
No there is no rule against declaring variables after an echo statement

Parse error: syntax error, unexpected T_VARIABLE on line 5 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am new to PHP and am encountering this parsing error. My code is:
<?php
$con=mysqli_connect("glcmcrusaders.ipagemysql.com", "cycregister", "Crusaders13!",
"registration")
$sql="INSERT INTO camperregistration (camperFirstName, camperLastName, camperGender,
camperBirthdate, camperStreet, camperCity, camperState, camperZip, camperCountry,
camperShirtSize, guardianFirstName, guardianLastName, guardianStreet, guardianCity,
guardianState, guardianZip, guardianCountry, guardianCell, guardianEmail,
alternateFirstName, alternateLastName, alternateStreet, alternateCity, alternateState,
alternateZip, alternateCountry, alternateCell, alternateRelationship, camperSwimming)
VALUES
('$_POST[camperFirstName]', '$_POST[camperLastName]', '$_POST[camperGender]',
'$_POST[camperBirthdate]', '$_POST[camperStreet]', '$_POST[camperCity]',
'$_POST[camperState]', '$_POST[camperZip]', '$_POST[camperCountry]',
'$_POST[camperShirtSize]', '$_POST[guardianFirstName]', '$_POST[guardianLastName]',
'$_POST[guardianStreet]', '$_POST[guardianZip]', '$_POST[guardianCountry]',
'$_POST[guardianCell]', '$_POST[guardianEmail]', '$_POST[alternateFirstName]',
'$_POST[alternateLastName]', '$_POST[alternateStreet]', '$_POST[alternateCity]',
'$_POST[alternateState]', '$_POST[alternateZip]', '$_POST[alternateCountry]',
'$_POST[alternateCell]', '$_POST[alternateRelationship]', '$_POST[camperSwimming]')";
mysqli_close($con);
?>
Line 5 starts at the %sql ="INSERT INTO... ends at camperSwimming)
I am not sure what I am doing wrong.
Missing a semi-colon:
$con=mysqli_connect("glcmcrusaders.ipagemysql.com", "cycregister", "Crusaders13!",
"registration") // <-- HERE
you need a terminator(;) at the end of $con.
$con=mysqli_connect("glcmcrusaders.ipagemysql.com", "cycregister", "Crusaders13!",
"registration");
^Here

$variable = $url + static_page.php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I would like to uplaod files using cURL but can't figure out how I can use $url array.
For example:
$urls = array("http://images.domain.com/",
"http://flash.domain.com/",
"http://other.domain.com/"
);
foreach ($urls as $url) {
I'm trying this but without success:
$upload = "$url ."upload/upload.php";
Any advice? :)
Thanks
You need to take out the first quote for it to work
$urls = array("http://images.domain.com/",
"http://flash.domain.com/",
"http://other.domain.com/"
);
foreach ($urls as $url) {
$upload = $url ."upload/upload.php";
}

Categories