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 6 years ago.
Improve this question
I receive the following error message when I attempt to call a function which I need to push an object into an array:
array_push() expects parameter 1 to be array, null given
Any clues why this is happening? Thank you in advance :)
<?php
$programming = array();
//some unrelated lines of code here inbetween
function createProgramming($data){
global $programming;
$prog = new Programming($data);
array_push($programming, $prog);
}
?>
//random HTML here
<php?
createProgramming("str");
?>
//more html
$programming is only referenced in the code at those three locations present in my extract above.
That code works fine. There are few things that could make it break:
$programming is redefined / unset before createProgramming() is called
$programming is not defined in the global scope
Related
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.
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 3 years ago.
Improve this question
I have a very simple web app that is capturing RFID tag reads and then submits it into the Database.
I have a function that was to pass the information through a filter and remove the duplicates and then return an array of unique tag reads.
The function looks like this
$txtarea = $_POST["rfid"];
rfid($txtarea);
function rfid($txtarea){
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
return $rfid_array1;
}
I then use Print_r to check the contents of the array to make sure it works.
When I run the code inside the function I do not get a result returned but when I run the following outside the function
$txtarea = $_POST["rfid"];
rfid($txtarea);
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
It returns the values correctly ?
I am very new to PHP so I apologize if this question seems a little basic.
The function rfid returns a value which you could capture in a variable.
$rfid_array1 = rfid($txtarea);
Note that you could shorten the function a bit:
function rfid($txtarea){
return array_unique(explode("\r\n", $txtarea));
}
Demo on https://3v4l.org/DY8Ts
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 6 years ago.
Improve this question
I'm getting this error and I can't make head or tail of it.
The exact error message is:
function kdrusha_theme_create_page() {
require_once(get_template_directory().= '/inc/pages/kdrusha-settings.php');
}
add_menu_page("KD Rusha Options", 'KD Rusha', 'manage_options', 'kdrusha-options', 'kdrusha_theme_create_page','',99);
The problem is that you're using .=.
something .= something_else
is shorthand for
something = something . something_else
But your something is a function call, and it generally doesn't make sense to assign to a function call (the exception is when it returns a reference).
You should just use ., which concatenates its parameters and returns the result without assigning it anywhere.
require_once(get_template_directory() . '/inc/pages/kdrusha-settings.php');
You need to put your function return in some variable:
function kdrusha_theme_create_page() {
$template = get_template_directory();
require_once($template.'/inc/pages/kdrusha-settings.php');
}
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
I'm having to put together a PHP snippet involving capturing form submission data. I'm trying to do this the "right" way with OOP PHP, and I'm struggling with array usage since my Googling seems to bring up sample code that is either too simple or too complex for what I'm trying to do.
How do I create a class method that adds keys and values to an array?
My code, which does not work currently:
class Connection
{
private $postItem = array();
public function addPostItem($key,$value)
{
$postItem[$key] -> $value;
}
public function printPostItem() {
return $this->postItem;
}
}
$c1 = new Connection;
$c1->addPostItem('FirstName','John');
$c1->addPostItem('LastName','Doe');
$c1->addPostItem('Email','JohnDoe#mail.com');
var_dump($c1->printPostItem()); // shows no array content
If I'm going against other best practices here, please let me know.
Use this:
public function addPostItem($key,$value)
{
$this->postItem[$key] = $value;
}
Change:
$postItem[$key] -> $value;
To:
$this->postItem[$key] = $value;
Also as Amal said turn on error reporting.
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 9 years ago.
Improve this question
I'm currently having an issue with simplexml_load_file(); my xml path is a url, that is rendered as a variable
$xurl = "domain/pathto/myfile.xml"; // This is actually a variable that returns the entire URL to where my xml file is -- this will change from file to file
$xmlpath = parse_url($xurl, PHP_URL_PATH); // to get the path of my xml file ex. /pathto/myfile.xml
$xmlpath = mb_substr($xmlpath, 1); // returns pathto/myfile.xml
here is where my problem is, when I put it into :
simplexml_load_file($xmlpath);
In my function, I get nothing appearing from the XML file
However, in my same function if I change it to
simplexml_load_file("pathto/myfile.xml");
My function works fine.
I did an echo on $xmlpath and it returns the pathto/myfile.xml just fine.
<?php echo $xmlpath; ?> // returns pathto/myfile.xml
What am I doing wrong?
EDIT: Phil
echo strcmp("pathto/myfile.xml", $xmlpath)
returns a 0.