Unable to pass a parameter to my function? - php

I have the following in a common.php file:
$debug = true;
function debug_to_screen(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
print("Debug Mode: " + $value);
}
}
}
In my main file I then call this function with the following:
require("common.php");
if(!isset($_COOKIE["qcore"]))
{
debug_to_screen("Cookie not found: ", var_dump($_COOKIE['qcore']));
}
However I'm receiving the following error:
Fatal error: Cannot pass parameter 1 by reference in
C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\QCORE\login.php
on line 8
This is one of the first time's I've tried making a function that can be passed multiple values, so I don't have the skill set to understand why this isn't working. Basically - I'd like to make a debug function that I can pass multiple values to, that is defined in my common file.

Your problem is here:
debug_to_screen("Cookie not found: "...
You are passing to the function which is expecting the data to be passed by reference - meaning you are sending it the ACTUAL variable, not a copy of it.
You will need to make the array first, then pass i by reference to the function.
Something like this:
$array=$_COOKIE;
debug_to_screen($array);
In your function, you defined it as:
function debug_to_screen(&$array)
The extra & means the function is taking the ACTUAL variable, not a copy of it.
This will also not work:
function julie(&$bob)
{
// something..
}
julie(5);
This is because although the function can change the 5, it cannot be returned via the reference pass.
$var1=5;
julie($var1);
This would have worked perfectly well, as $var1 can be modified and the external code calling the function can use the changed variable.

You function function debug_to_screen(&$array) accepting only one argument and however you are sending text as first argument and cookies as second argument.
So
replace
debug_to_screen("Cookie not found: ", var_dump($_COOKIE['qcore']));
to
debug_to_screen($_COOKIE['qcore']);

There's no need to pass by reference, and you're passing two arguments when only one is defined. You're also trying to print an array, var_dump prints not returns.
I think you need to look at the PHP docs for the functions you're using, there are a number of issues here.

Related

Getting a warning about user_func_array in WordPress PHP...?

I am trying to pass variables from a survey onto a wordpress website via URL. While the variables do successfully pass, I also get this message:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /home4/insig14/public_html/wp-includes/class-wp-hook.php on line 286
I know that it's either an "add_action" or add_filter" issue, and the only issue I can think of is in the code I added to the functions.php :
for ($x=1;$x<=38;$x++){
$a = "tsati";
$b = $a . $x;
add_action('init', add_get_val($b));
}
function add_get_val($b){
global $wp;
$wp-> add_query_var($b);
}
There are 38 variables I'm passing with each variable being "tsatiX" (ex. tsati1, tsati2...). The variables successfully are passed, but the error keeps appearing on top of the website. It's probably a problem with my "add_action" function, but I'm not sure what it is. Any help?
Your mistake is on this line:
add_action('init', add_get_val($b));
This function is intended to register a callback to be run when WordPress is processing 'init' actions - the second argument should be the callback to run. But you are actually running the function, so passing in its result.
This might be clearer if we split the line in two with a temporary variable:
$temp = add_get_val($b);
add_action('init', $temp);
So add_get_val is being run immediately, but since it doesn't return anything, null is being passed to add_action. WordPress doesn't check this, and so later tries to execute null as a callback, giving you the error.
If you want to register add_get_val as the callback, pass it in as a string:
add_action('init', 'add_get_val');
Alternatively, you might want an anonymous function to be the callback:
add_action('init', function() use ($b) { add_get_val($b); });
Or maybe it's fine to just run that code immediately, and not register it from the init hook at all, since your code seems to be working OK, in which case you can just run:
add_get_val($b);
The second parameter of add_action is supposed to be a callable, but you're passing null instead, because the add_get_val function doesn't return anything. If you meant to pass the add_get_val function instead of the result of calling that function, just pass its name as a string.
add_action('init', 'add_get_val');
I'm not sure if that is actually what you meant to do, but that's why you're getting that error anyway.

Using function in Wordpress to pars array value

Hi friends i get some array value from an other page and i should put this value in my 'wp-posts' table. For this i have created a function, which receive an array value and db connection value. Below you can see how i sent a value to this function.
foreach ($avaible as $listingx) {
AddPost(&$mysqli, $listingx);
}
And here belowe i try first to write in my log file this values.
function AddPost(&$mysqli, $listing){
foreach ($listing as $key => $value) {
mylog(" key ::".print_r($key, TRUE));
mylog(" value ::".print_r($value, TRUE));
}
}
Write in the log file had worked in the same file to other function. But in AddPost function is this not working .And when it come to "AddPost()" after that it's not working. Please can some one tell me why this function is not working.
You're using references wrong: the reference sign should be used on the function definition, not on the function call. So change this :
AddPost(&$mysqli, $listingx);
To this :
AddPost($mysqli, $listingx);
From PHP Doc:
There is no reference sign on a function call - only on function
definitions. Function definitions alone are enough to correctly pass
the argument by reference. As of PHP 5.3.0, you will get a warning
saying that "call-time pass-by-reference" is deprecated when you use &
in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was
removed, so using it will raise a fatal error.
If you had enabled WP_Debug you should have seen an error about this.

function to convert string to integer -- call to undefined function

I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit

PHP Function name must be a string error on new variable

I am getting the error:
Function name must be a string on this code:
$profile_data = $user_data('first_name','last_name','email');
Any ideas why this could be?
While you can use variables as function-names, to do so requires the variable to be a string.
The variable $user_data sounds more like an array, or even possibly an object. If this is true, you will receive the error specified. Per the comment from #Jon, it could also be possible that user_data() is a method and the $ is a typo.
If none-of-the-above helps, please all relevant code, specifically the creation of the $user_data variable (or a var_dump($user_data) output).
in php function define following
$profileData = user_date('first_name','last_name','email');
function user_date($first_name,$last_name,$email){
}

Codeigniter passing no URI segment

I'm using codeigniter for a project.
The URL is segmented like this:
example.com/admin/events
example.com/admin/events/add
I have a class called Admin which has a function called events
function events($data)
{
echo $data;
}
this will echo 'add' for the events/add page but when I try and access the events page alone i get an error
Missing argument 1 for Admin::events()
I thought that I could make two functions, one with a variable being passed and one without but apprently php doesn't allow that.
Fatal error: Cannot redeclare Admin::events()
How can I make this work?
Thanks.
Try that
function events($data = NULL)
{
echo $data;
}

Categories