<?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";
}
?>
Related
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.
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))
{
...
}
I have this script in php:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){
$data = $_POST['data_id'];
DoConfig($data);
function DoConfig($param_data){
echo $param_data;
}
}
}else{
echo '0';
}
I don't understand why I'm getting an error Call to an undefined function, how can I fix it?
PHP is executed sequentially - declare the function before using it and you'll be fine.
To elaborate - in PHP the entire file is loaded, and parsed based on scopes. If the function was at the end of the global scope this would work because at that point the global scope was evaluated before the subscope of the conditional was entered. Since you are entering a subscope with the if, the same evaluation order applies - the function needs to be evaluated before being used in its current scope.
Your code is failing because the function is declared inside your if() loop and after it is called. You could move it outside of the if() and still leave it at the bottom of the script, but best practice dictates otherwise.
Declare your functions before you use them, and outside of any conditionals or loops; preferably in a separate file or in the very least at the very top of the script. For example:
function DoConfig($param_data) {
echo $param_data;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['data_id']) && $_POST['data_id'] != NULL) {
$data = $_POST['data_id'];
DoConfig($data);
}
} else {
echo '0';
}
please put DoConfig function outside if-else condition
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){
$data = $_POST['data_id'];
DoConfig($data);
}
}else{
echo '0';
}
function DoConfig($param_data){
echo $param_data;
}
You need to declare your function before you call it. Do:
function DoConfig($param_data){
echo $param_data;
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){
$data = $_POST['data_id'];
DoConfig($data);
}
}else{
echo '0';
}
Well, tell me more.. in my real project, inside the function DoConfig i have a insert into the DataBase in PDO, so if i put these function outside the ''if's'' , have problems into the security?
*Creating function inside if statement is not a best practice * because that will be called if condition is true else will give undefined error in case you call that function later on. Also you called your function before it is even created thats why giving undefined error.
So better to create function outside if statement and run it anywhere.
//Creating function first and then calling it afterwards
function DoConfig($param_data)
{
echo $param_data;
}
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){
$data = $_POST['data_id'];
//Function call
DoConfig($data);
}
}else
{
echo '0';
}
EDITED as per your answer: There is absolutely no problem with security if you create it outside if statement, use functions when needed.
Alternatively for future purpose if you are creating function inside if statement then use
function_exist method later on so that you don't get undefined error
http://in1.php.net/function_exists
I have the following code to validate form data. I have created functions to validate various groups, and then have an if isset statement to check if these functions return true. I have tried many different ways to get this to work.
The problem I am having is this. I want the if isset to end if returning FALSE; but it doesn't, it keeps going and pops up the next alert (in my code I have many functions). How can I get it to exit after the first return FALSE? Do I need to make the isset into a function? So it can exit on return FALSE. thanks
I am having trouble writing a function to call functions in php.
function namecheck ($fname, $lname)
{
$regexp ="/^[A-Za-z]+$/";
//filter through names
if (preg_match($regexp,$fname,$lname))
{
return TRUE;
}
else
{
echo'<script type="text/javascript">alert("Enter your names.")</script>';
return FALSE;
}
}
function emailcheck ($email1, $email2)
{
$regexp="/^[a-zA-A-Z0-9_.]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$/";
//validate email address
if (preg_match($regexp,$email1,$email2))
{
return TRUE;
}
else
{
echo '<script type="text/javascript">alert ("Enter a valid email address.")</script>';
return FALSE;
}
}
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$namecheck=namecheck($fname,$lname);
$email1=$_POST['email1'];
$email2=$_POST['email2'];
$emailcheck=emailcheck($email1,$email2);
if (isset($_POST['submit']))
{
if ($namecheck !==TRUE)
{
return FALSE;
}
elseif ($emailcheck!==TRUE)
{
return FALSE;
} //and so on..
else
{
return TRUE;
}
}
A general structure for your functions you could follow is something like this:
function validateName($name) {
// Do Validation. Return true or false.
}
function validateEmail($email) {
// Do Validation. Return true or false.
}
function isFormValid()
{
// Name Validation
if( ! validateName( $_POST['name'] ) )
return false;
// Email Validation
if( ! validateEmail( $_POST['email'] ) )
return false;
// Form is valid if it reached this far.
return true;
}
// In your regular code on Form Submit
if( isset($_POST['submit']) )
{
if( isFormValid() ) {
// Save Form Data to DB
} else {
// Show Some Errors
}
}
That general structure should work fine for you. It could be made a LOT better but, for the sake of learning, this is sufficient.
If you want the script to, as you put, "exit" then you need to use exit(); Generally this is bad as the script will completely stop executing. Maybe you can look into using "break;" to get you out of a loop and stop executing functions within that loop. Another problem is that you are echoing out HTML code in your function which gets executed on assignment and so you will always get an alert generated when it evaluates to FALSE.
edit:
within your if(isset()) block. Inside here you can do{}while(false); which is a loop and will let you break out of it at anytime and prevent further execution of code within that loop.
If a function isn't returning false, then it never reached a return FALSE; statement. It's as simple as that. So let's examine the relevant code:
if (isset($_POST['submit']))
{
if ($namecheck !==TRUE)
{
return FALSE;
}
elseif ($emailcheck !== TRUE)
{
return FALSE;
} //and so on..
else
{
return TRUE;
}
}
So, if $_POST['submit'] is set and is not null, the if block will be reached. Then, if $namecheck is not true OR $emailcheck is not true, the function will return FALSE. You can simplify the above code to just:
if (isset($_POST['submit']))
{
return !(!$namecheck || !$emailcheck);
}
However, it doesn't look like this code is inside a function, so the return statement will do nothing. You have to put it in a function if you want it to work like a function.
Beyond that, I can't help you. I don't know what you want to do with this code. You seem to know how to use and call functions, so I'm not sure what the problem is. If you want to return from a function, put code in a function and call return. Right now your code is not in a function, so return won't do anything.
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);