Call function and pass parameter over url PHP - php

This is my PHP script:
<?php
function myFunc($param1)
{
echo $param1;
}
if(isset($_GET['action'])){
if(function_exists($_GET['action'])) {
$_GET['action']();
}
}
?>
Now I want call this function from other php and pass parameter:
http://localhost/data.php?action=myFunc
How to pass parameter to myFunc through url?

It is most certainly wrong and insecure (imagine what would happen if you tried calling it with action=unlink&param=somefile.php), but you could do something like:
With URL: http://localhost/data.php?action=myFunc&param=123
<?php
function myFunc($param1)
{
echo $param1;
}
if(isset($_GET['action'])){
if(function_exists($_GET['action'])) {
$_GET['action']($_GET['param']);
}
}

Related

What is the use of a variable function in PHP?

I understand how to implement a variable function though i don't understand it's use. Why call a function using a variable than to call the function itself?
Unless to dynamically call functions from user input or returned database results?
EXAMPLE : if you have an input like /?do=something
require_once('do.php');
$fun = 'do_'.$_GET['do'];
if (function_exists($fun)) {
$fun(); //variable function
} else {
not_found();
}
so in this case I just add a function to my do.php file and it will be ready to use
do.php :
<?php
function do_getkey() {
// do something when do=getkey
}
function do_sendkey() {
// do something when do=sendkey
}
function not_found() {
// when not found
}
?>

ajax response inside a function by ob clean

I'm looking for a way to calling a function by a function and get it's content executed in the function like:
function response($execute){
ob_clean();
$execute();
die();
}
so when i call, i want to give it a process as argument, like:
response(echo("hi"));
PHP using anonymous function.
PHP code demo
function response($execute)
{
if(is_callable($execute))
{
$execute("some-value");
}
else
{
echo "Not a function";
}
}
response(function($someVariable){
echo "Hi i am in anonymous function with 1st argument ".$someVariable;
});
response("Hi");

mail function not working in OO PHP

I tried the following:
a.php
<?php
class Page
{
function go(){
echo "1";
send();
}
function send(){
mail("whatever#gmail.com","subj","hi");
}
}
?>
b.php
<?php
require("a.php");
$page=new Page();
$page->go();
?>
b.php doesn't neither sends a mail nor t echo anything. When I put echo before send in the function go(), PHP echo "1" but doesn't send anything. I thought maybe there's something wrong with the mail() function so I changed b.php to:
<?php
require("a.php");
$page=new Page();
$page->send();
?>
and everything works fine. What is the problem with the initial code?
This isn't a mail function. You are trying to call a function that isn't labeled correctly. In this type you're attempting to call a static function. But the function you're calling is not labeled static. You can fix this a few different ways however.
Your echo doesn't work because it can't find the function you're referencing in go().
// best use
class Page{
public function go(){
echo "yay";
$this->send();
}
...
}
or
// static isn't recommended most often, but this will work.
class Page{
// same go function
public static function send(){
// same
}
}
Been awhile since I've used PHP, but you have to use $this to call the send method from a class method. See below:
<?php
class Page
{
function go(){
echo "1";
$this->send(); # Added the $this-> to the function call
}
function send(){
mail("whatever#gmail.com","subj","hi");
}
}
?>
Change send();
to
$this->send();
send() is a class method not a function, hence it cannot be called without $this in this case.

Why function is undefined in ajax file - does function order matter in AJAX file?

I am really very surprised to see my function comes out to be undefined on AJAX request. I am wondering does function order matters in AJAX file? Following code will show you my problem -
This is the ajax.php file which is called by jquery/ajax request from index.php. It is supposed to simply print the name -
<?php
if(isset($_POST))
{
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
}
?>
But when this file is called, i get -
Fatal error: Call to undefined function display_name()
But when i change the order of code i.e. function like this -
<?php
if(isset($_POST))
{
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
}
?>
then it displays -
admin
How strange it is!
Thus if really function availability order matters then how the following code works -
It is simple file and it is NOT called by AJAX request. I am simply loading the file and doesn't matter where the function is written. It is working by all using any order of line code -
<?php
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
?>
The following snippet is also working -
<?php
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
?>
So please tell my the reason behind this difference. In page loading code works and on ajax request it doesn't. Why the function display_name() is undefined if it exists?
This has nothing to do with Ajax.
From the PHP manual:
Functions need not be defined before they are referenced, except when a function is conditionally defined
The order matters in your first example because the function is inside an if statement.
I think this issue is not related to calling file as Ajax request. This is more related to PHP's function declaration scope.
Consider the following situation:
if(true)
{
function bar()
{
}
$functions = get_defined_functions();
print_r($functions["user"]);
}
function foo()
{
}
this code will give bar and foo as defined functions.
But the following situation produces just foo function:
if(true)
{
$functions = get_defined_functions();
print_r($functions["user"]);
function bar()
{
}
}
function foo()
{
}
From this we see that all the function in a file are defined and available imediately when the file is loaded, but if blocks are interpreted as the execution passes step by step.
Hope this helps.

PHP: method with same name in different classes not working

Same function name in different isolated classes is not allowed?
What am I doing wrong?
I reduced my real code to the minimum required to make some test.
Here it is:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
class confFunctions {
function getConf() {
function doWork() {
echo "I am from confFunctions!<br />";
}
doWork();
}
}
class thePage {
function loadPage() {
function doWork() {
echo "I am from thePage!<br />";
}
doWork();
}
}
// Start check.
echo "Checking...<br />";
$conf = new confFunctions();
$conf->getConf();
$page = new thePage();
$page->loadPage();
?>
The output is:
Checking...
I am from confFunctions!
Fatal error: Cannot redeclare doWork() (previously declared in /var/www/Test2/index.php:11) in /var/www/Test2/index.php on line 23
Renaming one of the shared-name functions makes all working well. That is, changing doWork to doWork1 in the second class, like this:
class thePage {
function loadPage() {
function doWork1() {
echo "I am from thePage!<br />";
}
doWork1();
}
}
gives correct results:
Checking...
I am from confFunctions!
I am from thePage!
Should not what is inside a class be visible only to that class, if not declared public?
By declaring a function in a function, you are actually declaring the second function into the global scope.
If you want your functions to be limited to the class scope, don't declare a function in another function, but rather declare them under each other.
Consider this code that declares a function in another function (in a class):
<?php
class MyFunctions {
function load() {
function doWork() {
echo "I am doing my work from global scope";
}
}
}
$mf = new MyFunctions();
$mf->load();
// $mf->doWork(); <-- won't work here
doWork(); // <-- this will work!
?>
Now consider this code that declares a function under another function (in a class).
<?php
class MyFunctions {
function load() {
//...
}
function doWork() {
echo "I am doing my work from class scope";
}
}
$mf = new MyFunctions();
// $mf->load(); <-- not really important anymore
$mf->doWork(); // <-- this will work now
// doWork(); <-- won't work here anymore
?>
Function scope is always namespace wide when declaring a named function.
You'll need to assign it to a variable to constrain it to a specific scope ($doWork = function() { }).
You seem to be going down an odd path though. Perhaps you should just use a private method?
Full example just to make it clear:
class confFunctions {
function getConf() {
$doWork = function() {
echo "I am from confFunctions!<br />";
};
$doWork();
}
}
class thePage {
function loadPage() {
$doWork = function() {
echo "I am from thePage!<br />";
};
$doWork();
}
}
I dont think you meant to nest the functions ? and your calling them from the global scope.
something like this is likely what you meant
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
class confFunctions {
function getConf() {
$this->doWork();
}
function doWork() {
echo "I am from confFunctions!<br />";
}
}
class thePage {
function loadPage() {
$this->doWork();
}
function doWork() {
echo "I am from thePage!<br />";
}
}
// Start check.
echo "Checking...<br />";
$conf = new confFunctions();
$conf->getConf();
$page = new thePage();
$page->loadPage();
?>
First guess would be that somehow you aren't properly closing your class from the first example. Different classes are definitely allowed to have the same function names, so there's something else going on in your code here that's not being shown through the psuedo-code you're posting.
UPDATE:
As NL-X said, by posting the function inside of a class function it then creates it in global scope. Thank you for updating your pseudo-code with actual examples.

Categories