netbeans not stepping through the function being called - php

In my php, I call a function if a certain value is true,
if( 1 == 1 ) {
my_function( variable );
}
I can step through the if statement and see what is going on, but for some reason, the step through, goes on the function call line, and it does not step through the actual function code. It just highlights the function call, then finishes off successfully.
Why is that happening? I would have thought it would show me the internal workings of the function but steping through the function being called?

"Step into" should enter the function.

Add breakpoint in the 1st line of function code you want to step through and try to debug.

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.

Proper way to 'exit' a script in PHP

I have a static method named ServerResponse that basically shows a message whether on success or fail. I just want to know the proper way to actually display the message and exit the script.
Should I implement my method like this:
public static function ServerResponse($e,$m){
print(json_encode([$e,$m]));
exit;
}
//Sample use:
if(this happens){
myclass::ServerResponse($x,$y);
}
or like this:
public static function ServerResponse($e,$m){
return json_encode([$e,$m]);
}
//Sample use:
if(this happens){
print(myclass::ServerResponse($x,$y));
exit;
}
Which one is proper and better... and why?
is there any difference between them? (on execution time).
"Don't be hard on me I am not an expert (just yet)..."
For better debugging, it's advised to always make a function or method return a value. So your 2nd sample should be chosen.
exit (or die) are commonly used when the program ends with an error, giving the ability to add an exit status (as a number or a string).
I suppose there will be no significant difference about the execution time.
I don't know if this is common practice but I only use exit to debug. If your script comes to the end of execution it will exit on it's own.
If you must exit, do it in the main script, not in a function.
functions should have one task and do that task. So the function ServerResponse should send a response and not kill the script.

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 - creating a function, to use later in same page

I am trying to add a function named "addNotification" to my page "functions.php"
My function:
function addNoti($text,$userid)
{
mysql_query("INSERT INTO notifications (time,text,userid) VALUES('".time()."','$text','$userid')");
if(mysql_affected_rows() != 1)
return 2;
else
return 100;
}
A couple of hundreds line BELOW that^ (same page), I have my registration function "register":
function doRegister()
{
global $datesetting;
mysql_query("query to register goes here");
addNoti("You just joined us!","$userid");
}
Although, whenever I process my registration form, I get the following error:
Fatal error: Call to undefined function addNoti() in /home/domain/public_html/path/to/functions.php on line 278
(Line 278 is where the addNoti is called)
Whenever I try to move addNoti to another page, and load that page in functions.php I get no error.
Please help.
Thank you in advance.
Keep in mind that in PHP you can declare function in conditional block. It could be that in your code execution took the other branch and never got to the function definition. Check the code blocks, check if the code with the function definition gets executed (echo or var_dump are your friends)
Is it a typo? In your question you name the function addNotification and addNoti()...
Do you call doRegister() before addNoti() occurred in your script?! That is the question!
From the two functions only it is hard to say what the problem can be. Did you accidently put the function addNoti inside another function (before the closing bracket)?

Strange behavior in a codeigniter library

I've made an incredibly basic library, with a single function to check if the user is logged in now or not.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Lib {
public function is_logged_in()
{
$CI =& get_instance();
if($CI->session->userdata('uid') === FALSE)
{
return FALSE;
}
else
{
return TRUE;
}
}
}
/* End of file Lib.php */
If I call the function from a controller, the code is definitely run and when not logged in it makes it into the if statement. I can add an echo to test that the code is definitely executed, but never is anything returned. If I change the return to a number, nothing is returned. If I change the return to a string, then and only then is stuff actually returned. I'm calling the function like $this->lib->is_logged_in(), and have added the library to the autoload.php file.
The return is definitely executed as the function is exited. error reporting is set to E_ALL. Why in the heck won't this work?
(also yes I do realize the function isn't complete and secure yet.)
How are you determining that the return is executed? – NullUserException
#NullUserException: The code block exits. I can echo prior to either return and it will run, I can echo after either and it will not. – Cyclone
If you are trying to echo something after the return in your function, it will not work by design, I think this is probably universal to all languages. Anything after the return will not be executed. It will be parsed, but not executed.
var_dump($this->lib->is_logged_in()) should give you bool(true) or bool(false), or a "Trying to get property of non-object" error if lib is not loaded correctly.
Unless there is something you aren't sharing with us, the function should work as expected.
If you're still in doubt, assign the return value to a variable, then var_dump() the variable before you return it. Should be the same result.
EDIT: Sorry, I missed this in the long stream of comments:
var_dump() reveals that false is indeed being returned, why can't I simply echo it
I don't believe that echo'ing FALSE should give you any output, but echoing TRUE should give you 1.
This also has nothing to do specifically with Codeigniter.
For all practical purposes, there's no good reason to echo the return value of this function, or really anything where you expect a boolean return value. If it is returning the value you expect, then it is working.

Categories