Using function in Wordpress to pars array value - php

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.

Related

Conert Php 5.6 to 7.2 getting error - Too few arguments to function Mymodel::mymodelfunction(), 0 passed in

My project running on PHP 5.6.
But now I need to covert this project in PHP 7.2.
So when I converted it in PHP 7.2 then I have facing argument initialization error
"Too few arguments to function Mymodel::mymodelfunction(), 0 passed in"
Because this argument is not defined as the default value.
This is not working on local environments give the same error.
But when I run this code in the server then this is working fine.
So please suggest me,
what can I do in local environments to strict error reporting.
You can simply change the signature from:
class Mymodel
{
public static mymodelfunction ($argument1)
{
(...)
}
}
to
class Mymodel
{
public static mymodelfunction ($argument1 = '')
{
(...)
}
}
make sure to pass correct fallback value (sometimes array might be needed, not string). This way you will provide a fallback (default) value if you don't pass params.
More details about the core source of your issue is available on PHP manual page:
http://php.net/manual/en/migration71.incompatible.php

Blank page if I declare(strict_types=1); in PHP 7 at top of the file

Recently I was checking out on PHP 7, specifically return type declaration and type hinting. I have compiled PHP 7 from source(master branch from Github) and running it in Ubuntu 14.04 virtual box. I tried to run following code to get a test of new Exceptions. But it Gave a blank page.
<?php
function test(): string {
return [];
}
echo test();
Then I realize I have to set error to be displayed on screen. So I added old fashioned ini_set('display_errors', 1); like below,
<?php
ini_set('display_errors', 1);
function test(): string {
return [];
}
echo test();
that gave me following TypeError as expected according to this Throwable interface RFC
Fatal error: Uncaught TypeError: Return value of test() must be of the
type string, array returned in /usr/share/nginx/html/test.php on line
7 in /usr/share/nginx/html/test.php:7 Stack trace: #0
/usr/share/nginx/html/test.php(10): test() #1 {main} thrown in
/usr/share/nginx/html/test.php on line 7
Digging further I added declare(strict_types=1); at the top as below,
<?php declare(strict_types=1);
ini_set('display_errors', 1);
function test(): string {
return [];
}
echo test();
and bang, error just got disappeared leaving me with blank page. I cant figure out why it is giving me a blank page?
After searching around the google and RFC's I came to follwing sentence in RFC,
This RFC further proposes the addition of a new optional per-file
directive, declare(strict_types=1);, which makes all function calls
and return statements within a file have “strict” type-checking for
scalar type declarations, including for extension and built-in PHP
functions.
This means there was nothing wrong with directive declare(strict_types=1) but the problem was the way I was calling ini_set() function. It expects second parameter to be of string type.
string ini_set ( string $varname , string $newvalue )
I was passing int instead, and hence the setting needed to display errors itself failed to set and hence I was hit
with a blank page by PHP strict mode. I then changed the code a bit and passed the string "1" as below and it worked.
<?php declare(strict_types=1);
ini_set('display_errors', "1");
function test(): string {
return [];
}
echo test();
as the error states your function expect you to return string but instead you return an array! And function complains which is normal. So on your return simply put some string value. That's it!

Unable to pass a parameter to my function?

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.

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

"Call-time pass-by-reference has been removed"

I'm trying to deploy Wordpress on Dotcloud using this repo but there is an error that appears in the logs:
18:59:19: [www.0] Running postinstall script...
18:59:21: [www.0] PHP Fatal error: Call-time pass-by-reference has been removed in /home/dotcloud/rsync-1353715101184/dotcloud-scripts/feed-wp-config.php on line 86
Looking at line 86 in feed-wp-config.php, it reads:
$content = preg_replace('/(define\(\'' . $property . '\', \')(.*)(\'\);)/', '${1}' . $value . '${3}', $content, -1, &$count);
When I go to the Wordpress start page it says, "There doesn't seem to be a wp-config.php file. I need this before we can get started."
I've cross-posted this to the repo's Github issue tracker, but as there hasn't yet been a response I'm posting it here as well in hopes that someone knows the answer.
Replace &$count with just $count. & meant you want variable to be passed by reference, not value:
Documentation says
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);.
So if you want to pass variable by reference to the function, you should use & in function declaration:
This now should be done that way:
// right
function foo(&$var) {
...
}
foo($foo);
but not that way (as you get this warning):
function foo($var) {
...
}
foo(&$foo); // <--- wrong
Remove the & sign from the &$count at the end of the line.
Please bear in mind, that this is a core hack in wordpress, and it will be lost on an update..

Categories