I've try to call a function in php. I've connected with an ajax call to the file (works fine). My question in now how can I call the function?
if (isset($_POST['callFunc1']) && !empty($_POST['callFunc1'])) {
echo 'get the echo';
function generateWigaNews()
{
echo 'don't get the echo';
}
}
if (isset($_POST['callFunc1']) && !empty($_POST['callFunc1'])) {
echo 'get the echo';
function generateWigaNews()
{
echo "don't get the echo";
}
generateWigaNews() //call function
}
Related
I cant get returned value from php function call.
...
$dey=$validate->yoxla($this->ad,$this->soyad,$this->istadi,$this->sifre,$this->email);
if($dey){ // i cant get returned value from here. even it is true.
echo 'asd';
if(!$checkuser->checkusername($this->istadi)){
$err=array();
$err['code']='code14';
$err['aciglama']=$code14;
print json_encode($err);
die();
}elseif(!$checkuser->checkemail($this->email)){
$err=array();
$err['code']='code15';
$err['aciglama']=$code15;
print json_encode($err);
die();
}else{
$err=array();
$err['code']='acig';
print json_encode($err);
die();
}
}
yoxla() function:
...
elseif (!preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $sifre)){
$err=array();
$err['code']='code11';
$err['aciglama']=$code11;
print json_encode($err);
die();
}elseif($email==""){
$err=array();
$err['code']='code12';
$err['aciglama']=$code12;
print json_encode($err);
die();
}elseif (!$this->validEmail($email)) {
$err=array();
$err['code']='code13';
$err['aciglama']=$code13;
print json_encode($err);
die();
}else{
return true;
}
I call first one by ajax request. and there is nothing returning to ajax or i cant get returned value from getresult() function.
Remove () from From class declaration :
try this :
class Form{
public function getresult(){
return true;
}
}
$validate=new Form();
$result=$validate->getresult();
if($result){
echo 'true';
}else{
echo 'false';
}
Enable php error logging so that you could understand such errors on browser : http://php.net/manual/en/function.error-reporting.php
Hello Id like to know how to call the function I just written from URL?
Bellow are my php code.
<?php
require 'db.php';
function l1(){
echo "Hello there!";
}
function l2(){
echo "I have no Idea what Im doing!";
}
function l3(){
echo "I'm just a year 1 college student dont torture me sir!";
}
?>
I tried http://127.0.0.1/study/sf.php?function=l1 but it wont echo the written code.
Please point me to the right direction.
Yes you could supply that parameter into calling your user defined function:
$func = $_GET['function'];
$func();
Might as well filter the ones you have defined thru get_defined_functions
function l1(){
echo "Hello there!";
}
function l2(){
echo "I have no Idea what Im doing!";
}
function l3(){
echo "I'm just a year 1 college student dont torture me sir!";
}
$functions = $arr = get_defined_functions()['user']; // defined functions
if(!empty($_GET['function']) && in_array($_GET['function'], $functions)) {
$func = $_GET['function'];
$func();
}
Sample Output
Sidenote: function_exists can be also applied as well:
if(!empty($_GET['function']) && function_exists($_GET['function'])) {
// invoke function
}
One option you can do if use if/elseifs like so:
if($_GET['function'] == 'l1')
{
l1();
}
else if($_GET['function'] == 'l2')
{
l2();
}
Or you could use a riskier approach and call the function name directly from the input.
$functionName = $_GET['function'];
$functionName();
Edit:
Or you could use a switch statement:
switch($_GET['function'])
{
case 'l1':
l1();
break;
case 'l2':
l2();
break;
case 'l3':
l3();
break;
}
Please check this below code. After for loop remaining code is not executing. It suppose to print "Helo", but it is not printing any thing.
for($i=0;$i<10;$i++)
{
$minrate=$obj_iScripts->min_avg_rate($roomnumber[$id_array[$i]], $amount_ary[$id_array[$i]], $totalrooms);
$all_min_price[]=$minrate;
if($_SESSION['star'][$id_array[$i]]>=1 && $_SESSION['star'][$id_array[$i]]<=5)
{
//include 'searchresult_table.php';
}
}
echo "Helo";
code:
public function min_avg_rate($roomnumber,$rates,$totalrooms)
{
$ary_name='iArray';
$total=0;
for($i=1;$i<=$totalrooms;$i++)
{
${$ary_name.$i}=array();
$temp=max($rates);
for($j=0;$j<count($roomnumber);$j++)
{
if($roomnumber[$j]==$i)
{
if($temp>$rates[$j])
$temp=$rates[$j];
${$ary_name.$i}=$temp;
}
}
$total=$total+${$ary_name.$i};
}
return $total/$totalrooms;
}
From what code you have posted -the min_avg_rate() function within your class would seem to be malfunctioning
I have a class with a couple of methods
deleteUploadedFile() and currentUploadedFiles().
currentUploadedFiles(), basically loops over a session array and displays it on screen, simple as. Code sample:
function currentUploadedFiles()
{
if(isset($_SESSION['fileArray']) && $this->count > 0)
{
echo '<p style="clear:both">Current files uploaded list:</p>';
echo '<ol>';
foreach($_SESSION['fileListing'] as $key => $value )
{
echo '<li>'. $value .' [Remove File]</li>';
}
echo "</ol>\n\r";
echo "<p> Current file size allowance: ". $this->_returnRemainingSessionFileSize() ." of 8 MB";
} else {
echo '<p style="clear:both">No files have been uploaded yet</p>';
}
if($this->deleteUploadedFile() === true)
{
echo '<p>File has now been deleted from our records.</p>';
}
}
the deleteUploadedFile() method, basically when form is submitted it deletes file from the server and removes the entry from the session array. Sample code:
function deleteUploadedFile()
{
(int) $id = $_GET['id'];
(bool) $deleted = false;
if (file_exists($this->target_path.'/'.$_SESSION['fileArray'][$id]))
{
$_SESSION['fileSize'] -= $this->_checkSessionFileSize($id);
if (unlink($this->target_path.'/'.$_SESSION['fileArray'][$id]))
{
$deleted = true; //'<p>File has now been deleted from our records.</p>';
unset($_SESSION['fileArray'][$id]);
unset($_SESSION['fileListing'][$id]);
}
}
return $deleted;
}
my controller, basically checks if file id# isset, then checks if the array id# isset, then calls the deleteUploadedFile() method and then calls the currentUploadedFiles() method.
Question is, why when I var_dump $deleted var in deleteUploadedFile() I get bool(true) but inside the currentUploadedFiles() method I get bool(false). Sounds like I'm messing up the scope somehow?
Looks like $deleted is in the local scope of the delete function.
Something like the following should work.
class theClass
{
function __construct()
{
$this->deleted = false
}
function delete()
{
$this->deleted = true;
}
function upload()
{
var_dump($this->deleted);
}
}
i have created below function in a file info.php to debug variable & data during page load
class Info {
static function watch($what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
echo "<br/>METHOD:".__METHOD__;
echo "<br/>LINE:".__LINE__;
}
}
}
now i call this method from another page index.php, where i inculded info.php
in this file i want to debug POST array, so i write below code
class Testpost {
__construct() { // some basic intializtion }
function getPostdata($postarray) {
$postarray=$_POST;
Info::watch($postarray,'POST ARRAY', TRUE);
}
everything is working fine but method and LINE appears below
METHOD:Info::watch();
LINE:17 // ( where this code is written in Info class)
but i wantbelow to display
METHOD: Testpost::gtPostdata()
LINE:5( where this function is called in Testpost class)
so how do i do that if i put$more=TRUE in watch() then method and line number should be diaply from the class where it is called.
can i use self:: or parent::in watch method?? or something else
please suggest me how to call magic constants from other classes or is there any other method to debug varaibale?? ( please dont suggest to use Xdebug or any other tools)
You have to use the debug_backtrace() php function.
You also have below the solution to your problem. Enjoy! :)
<?php
class Info {
static function watch($what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
$backtrace = debug_backtrace();
if (isset($backtrace[1]))
{
echo "<br/>METHOD:".$backtrace[1]['function'];
echo "<br/>LINE:".$backtrace[1]['line'];
}
}
}
}
You can not use those constants from that scope. Check out the function debug_backtrace() instead. If it gives you too much info, try to parse it.
debug_bactrace is the only way you could totally automate this, but it's a "heavy-duty" function.... very slow to execute, and needs parsing to extract the required information. It might seem cumbersome, but a far better solution is to pass the necessary information to your Info::watch method:
class Info {
static function watch($whereClass,$whereLine,$what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
echo "<br/>METHOD:".$whereClass;
echo "<br/>LINE:".$whereLine;
}
}
}
now i call this method from another page index.php, where i inculded info.php
class Testpost {
__construct() { // some basic intializtion }
function getPostdata($postarray) {
$postarray=$_POST;
Info::watch(__METHOD__,__LINE__,$postarray,'POST ARRAY', TRUE);
}