PHP Function name must be a string error on new variable - php

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){
}

Related

How to call functions in other PHP files?

I am trying to call a function from one file to another in PHP, however, I am getting the following error:
Fatal error: Call to undefined method SQLite3::print_model_query_form()
I have two files. The first - dbfunctions.php contains the method print_model_query_form().
The second file query_models contains the following code:
include_once("functions/dbfunctions.php");
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
print $db->print_model_query_form("query_models.php");
The function looks a little like this:
function print_model_query_form($action, $current_values = 0){
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
if($current_values){
// set to previous values.
}else{
// get POST values.
}
// Code to print query form.
}
Thanks in advance for any help.
Since you only hit fatal error on 3rd line, $db should be instantiated successfully. So, the issue should be the print_model_query_form method.
Referring to PHP:SQLite3 - Manual
, there is no such built in method called print_model_query_form.
[Edit 1]
Try to use require_once instead of include_once to make sure you have included dbfunctions.php successfully.
[Edit 2]
Check If you are using PHP's built in SQLite3 class (check your php.ini for extension=php_sqlite3.dll or extension=php_sqlite3.so).
If this is the case, check your dbfunctions.php for:-
class Something
new SQLite3
function print_model_query_form
If all the above exists then you should replace your 2nd line with,
$db = new Something(..);
Note: It would be better if you can show dbfunctions.php instead or letting us make assumptions based on guessing.

Pass an object attribute by reference

I have this php function to add an object to the database, but it is giving me some troubles to make it work. I'm pretty used to the java way, so i tried to follow the same pattern, and I'm getting an error message that says "Strict standards: Only variables should be passed by reference".
This is the code:
public function saveUser(User $user){
$this->connection = DaoFactory::openConnection();
$this->query = $this->connection->prepare($this->SAVE_QUERY);
$this->query->bindParam(':name', $user->getName());
$this->query->bindParam(':lastName', $user->getLastName());
$this->query->bindParam(':age', $user->getAge());
$this->query->bindParam('gender', $user->getGender());
$this->query->bindParam(':email', $user->getEmail());
$this->query->bindParam(':password', $user->getPassword());
$this->query->execute();
$this->connection = null;
}
I've searched and i found that the object attribute should be placed in a variable to avoid this issue, but by doing so, the code gets very messy and complex.Example:
$name = $user->getName();
$this->query->bindParam(':name',$name);
Is there any other way to solve this?
bindParam expects a variable passed by reference, you're instead giving it a value returned from a function, which cannot be passed by reference. So instead, use the other API method bindValue() to bind your values.
See https://stackoverflow.com/a/14413428/476 for what the difference is.

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

Yii clientScript Render Throws Error

I have the following in a partial view:
<?php
Yii::app()->clientScript->registerPackage('nivo-slider');
Yii::app()->clientScript->render();
However, I receive an error stating Missing argument 1 for CClientScript::render(),
What arguments does the render method require? I checked the docs but couldn't find anything definitive.
They seem pretty clear to me. You need to pass it a reference variable which will hold the value of the rendered string.
// as an example:
$output = NULL;
Yii::app()->clientScript->render($output);
// you can now do something with output!

Categories