PHP Function Parameter is always null in Wordpress - php

I am not an expert Wordpress PHP developer but I am not understanding this situation. I am passing a variable through a static function into the $GLOBALS variable that contains an array. That variable when in the static function is always NULL, but before it goes into it, it is a valid type and prints fine.
functions.php
$badge_Id = get_comment_meta($comment->comment_ID,"badge_id", true);
if(strlen($badge_Id) > 0) {
Cisco_Rewards::add_badge_id($badge_id);
echo $badge_id; // PRINTS PERFECTLY
}
rewards.php
class Cisco_Rewards {
static function add_badge_id($badge_id) {
if(count($GLOBALS['badge_ids']) == 0) {
$GLOBALS['badge_ids'] = array();
}
echo $badge_id; // WONT PRINT, IS NULL
array_push($GLOBALS['badge_ids'], $badge_Id);
print_r($GLOBALS['badge_ids']); // ALWAYS HAS NULL VALUES
}

Instead of
if(count($GLOBALS['badge_ids']) == 0) {
$GLOBALS['badge_ids'] = array();
}
echo $badge_id;
try
var_dump($badge_id); // to check what it contains at the very beginning of the function
if(!is_array($GLOBALS['badge_ids'])) {
$GLOBALS['badge_ids'] = array();
}

Related

How to access a member variable of different function in a codeigniter controller

//first function
function insertdigit(){
$userdigit=5;
$flag = $this->usermodel->userdigitmodel($userdigit);
$value = array(
'result' => $flag
);
echo json_encode($value);
if ($flag == true) {
return $userdigit;
} else {
}
}
//second function
function usedigit(){
$data['userdigit']=$this->insertdigit();
}
but i get {"result":true} goes back to the function? how to access a member variable in a different member function
Try to remove echo json_encode($value); in your code.
If you need to access a parameter in several functions on your controller, you have to create it outside your function so it will be available for all your controller functions.
So, in your case it should be something like this:
class Test extends Controller
{
private $userdigit; //here you can set a default value if necessary: private $userdigit = 5
function insertdigit(){
$this->userdigit=5;
$flag = $this->usermodel->userdigitmodel($this->userdigit);
$value = array(
'result' => $flag
);
echo json_encode($value);
if ($flag == true) {
return $this->userdigit;
} else {
}
}
//second function
function usedigit(){
$data['userdigit']=$this->userdigit;
}
}
This way your userdigit variable is available for all your functions. With $this you are telling PHP that you are trying to access something inside the class.
This link contain more and useful information: http://www.php.net/manual/en/language.oop5.properties.php
Is that what you really need?
A possible solution:
function insertdigit()
{
$userDigit = 5;
$flag = $this->usermodel->userdigitmodel($userDigit);
$value = array
(
'result' => $flag
);
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
echo json_encode($value);
}
if ($flag == true)
{
return $userdigit;
}
else
{
}
}
//second function
function usedigit()
{
$data['userdigit'] = $this->insertdigit();
}
The above code, in insertdigit detects if there is an Ajax request and if so, it will echo out the json_encoded data. If you call it in an normal request, i.e. via usedigit it won't echo the json_encoded data (unless you are calling usedigit via an Ajax request).
Your question doesn't really explain what you are doing, so it's hard to explain a better solution, however, if you are trying to access a "variable" in more than one place, you should really separate your code so you have a single entry point for that variable.
Is your variable dynamic, or is it static?

trouble with recursion in php

I cannot figure out why this function is not working. Evertime I try to make the recursive call all I get is an IE page with a cannot display error message. I left // by the lines that is causing me the trouble. I also tried the call without the $this-> and got an error function not recognized
private function insert($key, $current) {
$newnode=new Node($key);
$parent=$this->root;
if($this->root==null) {
$this->root=$newnode;
return;
} else {
if($newnode->data > $parent->data) {
$parent=$parent->rightChild;
$this->insert($key, $parent);//if I comment this line it
//work, but that make the function useless
} else {
echo "smaller ";
}
}
}
The error is obviously an infinite recursive loop.
This is most probably due to the fact that you never use the $current argument.
You're always comparing the $newnode->data against $this->root->data which if greater once, will always be greater.
Update
Here's how I'd change it
private function insert($key, $current = null)
{
$newnode = new Node($key);
$parent = null === $current ? $this->root : $current;
if (null === $parent) {
$this->root = $newnode;
return;
}
if ($newnode->data > $parent->data) {
// same as before from here

Does PHP grok null references?

I want to define a PHP function with an reference parameter, by default, a null reference. Not very much unlike what the following C++ code would do:
ReturnType my_function(moar lulz, ParameterType* ptr_to_my_param = 0)
{
// do some processing using lulz only
// ...
if (ptr_to_my_param)
{
// use *ptr_to_my_param
// or ptr_to_my_param->
// ...
}
// do more processing, again using lulz only
// ...
}
How do I do that in PHP?
If I understand you correctly, you want to do something like
class NiceClass {}
function foo($baz, NiceClass $bar = NULL) {
// Do some processing
if(bar !== NULL) {
// Do something with bar
$bar->yay($baz);
}
// Do some more processing
}
function my_function($value, $value = null) {
if(is_null($value)) {
} else {
}
}

PHP and DRUPAL, Cannot save values in a static variable and pass through function

Developing a module for drupal and I need to pass/modify variables within functions. I avoided using global variables because drupal uses the include function which subsequently makes my global variable into local.
As such, i created the following script which stores a static variable but I cannot retain the new value. Any help will be appreciated
function _example_set_flashurl($value = '21224', $clear = NULL) {
static $url;
if ($clear) {
// reset url variable back to default
$url = null;
}
// assigned url a perminate value within this function
$url = $value;
return $url;
}
function _example_get_flashurl() {
return _example_set_flashurl();
// retrieve the value inside set scope
}
_example_set_flashurl('another', TRUE);
print _example_get_flashurl(); // prints 21224, I want it to print another
Try this
<?
function _example_set_flashurl($value = '21224', $clear = NULL) {
static $url;
if ($clear) {
// reset url variable back to default
$url = null;
}
if($value!='21224') {
// assigned url a perminate value within this function
$url = $value;
}
return $url;
}
function _example_get_flashurl() {
return _example_set_flashurl();
// retrieve the value inside set scope
}
_example_set_flashurl('another', TRUE);
print _example_get_flashurl(); // prints 21224, I want it to print another
You override the value in the empty call to set in your get function.
First, you probably want to add the default value directly to the static and not the argument. Like this: "static $url = '21224';". Then, this value will also be returned when set has never been called.
Second, there is no need for a $clear argument if you can pass in any value you want. If you want to change it, just override the old value.
Third, as the answer from bruce dou showed, you want to protect it against accidently overriding the value.
So, this code for the set function should be all you need:
<?php
function _example_set_flashurl($value = FALSE) {
static $url = '21224';
// Only keep value if it's not FALSE.
if ($value !== FALSE) {
// assigned url a perminate value within this function
$url = $value;
}
return $url;
}
?>

"Fatal error: Cannot redeclare" when reusing xml parsing code

I have a single xml parsing function that I'm trying to call multiple times as I only need to strip a little data out and continue on.
Here is the function:
//Parse Product ID from Product Sides
function getProductSpecs($xml,$type) {
// Setup arrary
global $productspecs;
global $count;
$count = 0;
global $type_check;
$type_check = $type;
// Parse the XML
// Create the parser
if (! ($xmlparser = xml_parser_create()) )
{
die ("Cannot create name list parser");
}
// Start tag function
function first($parser, $name, $attribs) {
global $trigger;
if ($name == "PRODUCTSIDEID") {
$trigger = 1;
} elseif ($name == "PRODUCTID") {
$trigger = 1;
}
}
// data handler function
function xml($parser, $data) {
global $trigger;
global $productspecs;
global $count;
global $type_check;
if ($trigger == 1){
if ($type_check == "sideid") {
$productspecs[$count]=$data;
$count = $count + 1;
} elseif ($type_check == "productid") {
$productspecs[$count]=$data;
$count = $count + 1;
}
$trigger = 0;
}
}
// Call the handler functions
xml_set_element_handler($xmlparser, "first", "");
// Call the data handler
xml_set_character_data_handler($xmlparser, "xml");
// Parse the XML data
xml_parse($xmlparser,$xml);
// Clear parser
xml_parser_free($xmlparser);
//Return the array
return $productspecs;
}
My problem arises when this is called:
xml_set_element_handler($xmlparser, "first", "");
I get the redeclare error on:
function first($parser, $name, $attribs) {
The function only appears the one time and I'm assuming the problem occurs on the call but is there a way around this so I don't have to duplicate so much code. I'm going to have to iterate through this multiple times.
Thanks.
Defining functions inside of functions can lead to this. Each time you run getProductSpecs() it's going to try to declare first() and xml() again, and in PHP, all user functions are declared in a global scope. The best solution is to move your first() function and your xml() function outside of the main getProductSpecs() function.
Another option is to use function_exists() around your function declarations, like this:
if (! function_exists('first')) {
// Start tag function
function first($parser, $name, $attribs) {
global $trigger;
if ($name == "PRODUCTSIDEID") {
$trigger = 1;
} elseif ($name == "PRODUCTID") {
$trigger = 1;
}
}
}

Categories