Calling function in another function - php

I have a function printContent() which prints the arguments and logged() which checks if the user is logged in.
My point is to do something like this:
logged("printContent('TITLE', 'CONTENT', 1)", 1);
It doesn't work. It should printContent() if user is not logged in, but nothing is happening. If I'll try changing return() into print() it prints the text "printContent(text...)".
Here are these two functions:
function logged($echo = 0, $logout = 0)
{
if($_SESSION['user'])
{
if($echo)
{
if(!($logout)) return $echo;
else return false;
}
else return true;
}
else
{
if($logout == 1) return $echo;
else return false;
}
}
function printContent($title, $content, $type = 0){
if($type == 1){
echo '<div class="right-box">';
if($title) echo '<h3>'.$title.'</h3>';
echo $content.'</div>';
}
else {
echo '<div class="left-box">';
if($title) echo '<h2>'.$title.'</h2>';
echo '<div class="left-box-content">'.$content.'</div></div>';
}
}

It should printContent() if user is not logged in
You can try
if(!logged())
{
printContent('TITLE', 'CONTENT', 1);
}

Your logged() function is too complicated; the $echo and $logout parameters make the logic extremely hard to follow. You should simplify it to just this, doing one thing very well:
function isLoggedIn()
{
return !empty($_SESSION['user']);
}
Then, the logic becomes quite simple afterwards:
if (!isLoggedIn()) {
printContent('title', 'content', 1);
}
Play time
Being fancy, you could do this since 5.3, though it's a very contrived example of what you could accomplish with anonymous functions:
function ifLoggedIn($loggedIn, $loggedOut)
{
return empty($_SESSION['user']) ? $loggedIn() : $loggedOut();
}
The $loggedIn and $loggedOut parameters are the callback parameters and get executed from inside the function, based on whether the user is logged in or not. To use it:
ifLoggedIn(function() {
}, function() {
printContent('TITLE', 'CONTENT', 1);
});

What you seem to be looking for is a callback.
If I understand you correctly, you wish printContent only to execute, if the user is logged in, correct?
You may want to check this question for further info: How do I implement a callback in PHP?

you are trying to execute a plain string. Use
function1( function2() );
that is the same as
$tmp = function2();
function1($tmp);

Related

Integer is returned instead of boolean in php

I hope you are doing great. I have a class in my project with various methods. The thing that happens is, one of the methods is supposed to return a Boolean variable. Instead. It returned the number "1" instead of false. If you could tell me where is the problem.
Thanks in advance, Cheers.
Useful pieces of my code:
Class method:
public function validatePwd($pwd1, $pwd2) {
if (strcasecmp($pwd1, $pwd2) == 0)
{
return true;
}
else
{
return false;
}
}
The script that executes it:
$check1 = $user->validatePwd($Password, $Password1);
echo $check1;
if ($user->validatePwd($Password, $Password1))
{
}
else
{
$errors[] = 'Error!, passwords entered are not compatible, Please'
. ' enter passwords that match each other';
}
If you do var_dump($check1) instead of echo $check1; it should show as boolean.

How to call php function with URL?

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;
}

Php function include

So, I'm working on a dynamic forum signatures script and I alredy got it working. Now I will to make it so that only a specific user group can a specific design.
This is the function I made.
function userGroup($rank)
{
if ($rank == 38)
{
return true;
}
else
{
return false;
}
}
And used it like this.
if ($userGroup == true)
{
...
}
else echo('User with that ID doesn\'t exist in the database');
}
else echo('This user hasn\'t in the specific user group');
But it wont work like that.
Regards,
Lazar!
You made a function function userGroup($rank) and you are trying to call it as if its a variable $userGroup. What happened to the parameter? Your if-statement should be something like:
if (userGroup($var) == true) { ... }
You don't actually need the == true either:
if (userGroup($var)) { ... }
you can not use a function like this. you should have a function call with valid argument like this:
if (userGroup($intUserRank))
{
...
}
else echo('User with that ID doesn\'t exist in the database');
assume $intUserRank containing user rank.
You function could just be like this:
function userGroup($rank)
{
return ($rank == 38);
}
Then you would use it like this:
if (userGroup($some_rank))
{
...
}

PHP function not working

<?php
$name = $_POST['name'];
$namecheck = namecheck($name);
function namecheck($name){
if(strlen($name)>0)
{
return TRUE;
}
else if(strlen($name)==0)
{
return FALSE;
}
};
function control($namecheck1)
{
if ($namecheck == TRUE)
{
echo "It is TRUE";
}
else if ($namecheck == FALSE)
{
echo "It is FALSE";
}
};
?>
I wrote that there is no problem in HTML part, there is a problem in my php functions because I am new in PHP. Can you make it proper.
I think you will understand what I want to do in the functions its simple Im trying to do if it is true I want to see "it is TRUE" in the screen. Else .....
Take a look at the variables. They don't match:
function control($namecheck1)
{
if ($namecheck == TRUE)
You also never actually invoke that function.
You're not calling your 'control' function. try starting with
$name = $_POST['name'];
$namecheck = namecheck($name);
control($namecheck);
Also, your definition of you function is wrong (or the variable you use is). You can change the function to this
function control($namecheck)
Of the if's to
if ($namecheck1 == TRUE)
in the end the name after control( is the one you should check for in the if's
In your control function, the parameter is called $namecheck1 at first, but you only call it $namecheck when you try to use it inside the function.
It looks as if you are not calling control function.
your are referencing $namecheck in the function "control" but the parameter passed is named $namecheck1. $namecheck in the scope of function "control" is undefined.
Some tips:
instead namecheck() you can use empty()
before using $_POST['name'] you should check if it exists isset() should help
This works fine
<?php
$name = $_REQUEST['name'];
function namecheck($name1)
{
if(!empty($name1))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (namecheck($name) == TRUE)
{
echo "It is TRUE";
}
else if (namecheck($name) == FALSE)
{
echo "It is FALSE";
}
?>

Simple PHP Function Boolean Problem

Someone please explain to me why this doesn't work, and what I am doing wrong. For some reason, when I run the function validateUsername, the $error variable remains completely unchanged, instead of evaluating to true. How is this possible?
Yet, if I remove the code within the function and run it straight without a function call, it works. The example below is so simple it is practically pseudo code, and yet it doesn't work. Is this behavior unique to PHP? I don't want to run into this again in some other language.
<?php
$username = 'danielcarvalho';
$error = false;
function validateUsername()
{
if (strlen($username) > 10)
{
$error = true;
}
}
validateUsername();
if ($error == false)
{
echo 'Success.';
}
else
{
echo 'Failure.';
}
?>
This isn't working because $username isn't available within the scope of your validateUsername function. (Neither is the $error variable.) See the variable scope section of the PHP manual for more information.
You could fix this by adding global $username, $error; within your function, although this isn't a particularly elegant approach, as global variables are shunned for reasons too detailed to go into here. As such, it would be better to accept $username as an argument to your function as follows:
<?php
function validateUsername($username) {
if (strlen($username) > 10) {
return false;
}
return true;
}
if (validateUsername('danielcarvalho')) {
echo 'Success.';
}
else {
echo 'Failure.';
}
?>
$error has local scope in function validateUsername. To access global variables, use global keyword.
Read about scopes here. Change your function to:
function validateUsername($username)
{
global $error;
if (strlen($username) > 10)
{
$error = true;
}
}
validateUsername($username);
Better implementation using function parameter:
function validateUsername($username, &$error)
{
if (strlen($username) > 10)
{
$error = true;
}
}
validateUsername($username, $error);
Another implementation:
function validateUsername($username)
{
if (strlen($username) > 10)
{
return true;
}
return false;
}
$error = validateUsername($username);

Categories