Getting function variable in PHP - php

I have a function and I want to check the status of it and echo the return.:
if (insert($order, $items, $depot) !== false) {
echo json_encode(array('response'=>'success','message'=>'Order #'.$insert().' successfully placed.'));
}else{
echo json_encode(array('response'=>'danger','message'=>'Order failed'));
}
If the function passes it returns the order ID, if it fails it returns a fail. I am having trouble with the second line, how do I echo the value it is returning?

if (($orderId = insert($order, $items, $depot)) !== false) {
echo json_encode(array('response'=>'success','message'=>'Order #'.$orderId.' successfully placed.'));
}else{
echo json_encode(array('response'=>'danger','message'=>'Order failed'));
}
OR
$orderId = insert($order, $items, $depot);
if ($orderId !== false) {
echo json_encode(array('response'=>'success','message'=>'Order #'.$orderId.' successfully placed.'));
}else{
echo json_encode(array('response'=>'danger','message'=>'Order failed'));
}

Related

Loop until the request response message equals to "true" in php

I'm trying to make a php script that would make a loop that would get the contents of my site/server and if the text response is for example "false" then it would do the same thing, basically will loop until the site's text response will echo "true".
This is what i tried:
$getcontents = file_get_contents("http://example.com/script.php"); // it will echo false
if (strpos($getcontents , 'false')) {
$getcontents = file_get_contents("http://example.com/script.php");
else if (strpos($getcontents , 'false')) {
$getcontents = file_get_contents("http://example.com/script.php");
}
else if (strpos($getcontents , 'true')) {
echo "finished".;
}
I'm not sure if this is the right way or even if it is possible and i apologize in advance if i did not explain myself very well. Thank you for attention!
You could use a regular while loop.
$getcontents = 'false'; //set value to allow loop to start
while(strpos($getcontents , 'false') !== false) {
$getcontents = file_get_contents("http://example.com/script.php");
}
echo "finished";
This will loop until $getcontents does not contain false.
You could also use a recursive function like this.
function check_for_false() {
$getcontents = file_get_contents("http://example.com/script.php");
if(strpos($getcontents , 'false') !== false) {
check_for_false();
} else if(strpos($getcontents , 'true') !== false) {
echo "finished";
} else {
echo "response didn't contain \"true\" or \"false\"";
}
}
This function should keep calling itself until $getcontents contains the word true, and does not contain false.

If else condition, return value

I'm searching in user profile if there's a string inside appetit with the word demi.
$user = JFactory::getUser();
$profile = JUserHelper::getProfile($user->id);
$prixa = $profile->profile['appetit'];
if (strpos($prixa,'demi') !== false) {
$prix=6;
} else {
$prix=7;
}
seems to be working as expected. Below is a test case code I ran.
<?php
$prixa = 'emimore';
if (strpos($prixa,'demi') !== false) {
$prix=6;
} else {
$prix=7;
}
echo $prix;
?>

PHP array in callback smart contract web3.php

I try to read out a smart contract with web3.php, which works fine now, but I always only can read out a function, that returns a single value. When I call a function that returns for example a uint8 array, then I cannot call the elements of the array with ..[index].
Web3.php: (https://github.com/sc0Vu/web3.php)
That is my callback function:
$contract->at($contractAddress)->call($functionName, function ($err, $result) use ($contract) {
if ($err !== null) {
echo "error";
throw $err;
}
if ($result) {
$supply = $result;
echo $supply;
}
});
Has anyone an idea how I can receive an array in a callback in php?
You can see the answer in the author github "https://github.com/sc0Vu/web3.php".
$newAccount = '';
$web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
$newAccount = $account;
echo 'New account: ' . $account . PHP_EOL;
});

Why does in_array() always return false?

Try this code:
You use the URL with the parameter id e.g index.php?id=value
This value will be pushed into the data array that is maintained within a session.
I expect this test to always return true. But it always returns false, why?
if (in_array($id, $_SESSION['data'])) {
echo "$id in array";
} else {
echo "$id not in array";
}
The full code:
<?php
session_start();
//uncomment when need to clear the data array.
//if (isset($_SESSION['data'])) {
// unset($_SESSION['data']);
// die;
//}
if (!isset($_SESSION['data'])) {
$_SESSION['data'] = [];
}
if (isset($_GET['id'])) {
$id = strval($_GET['id']);
if (!in_array($id, $_SESSION['data'])) {
$_SESSION['data'][$id] = "data_$id";
}
echo '<pre>';
print_r($_SESSION['data']);
echo '</pre>';
// Why does this always return false?
if (in_array($id, $_SESSION['data'])) {
echo "$id in array";
} else {
echo "$id not in array";
}
}?>
Try isset() function.
Example:-
if (isset($_SESSION['data'][$id])) {
echo "$id in array";
} else {
echo "$id not in array";
}

PHP Function Stops Executing- no errors

I have been debugging some php code today and have run into a very strange problem. A function that I have to check if a password is valid stops executing part way through the function. No errors are generated either by PHP or by the web server itself.
Here is the function in question:
//Common Registration Functions
function checkPassword($password)
{
$bLen = strlen($password);
echo $bLen."\n";
echo $password."\n";
//Remove any illegal characters
$vPWord = preg_replace("/[^\!\#\#\\\$\%\&\*\-\_\,\.a-zA-Z0-9]/","",$password);
$aLen = strlen($vPWord);
echo $aLen."\n";
echo $vPWord."\n";
//If the password length before santization is different than after then the user used illegal characters
if ($bLen <> $aLen)
{
return "pass_charfail";
}
echo "pass length check 1 \n";
//Check sanitized password length
if (strlen($vPWord) < 6)
{
return "pass_short";
}
echo "pass length check 2 \n";
if (strlen($vPWord) > 10)
{
return "pass_long";
}
echo "pass length check 3 \n";
//Check password strength
$strength = 0;
if (preg_match("/[^a-z]/",$vPWord))
{
$strength += 1;
}
if (preg_match("/[^A-Z]/",$vPWord))
{
$strength += 1;
}
if (preg_match("/[^0-9]/",$vPWord))
{
$strength += 2;
}
if (preg_match("/[^\!\#\#\\\$\%\&\*\-\_\,\.]/",$vPWord))
{
$strength += 4;
}
if ($strength > 6)
{
echo $strength."\n";
return true;
}
else
{
echo $strength."\n";
return "pass_weak";
}
}
Here is the output I get from my error checking setup (my webhost will not enable php debugging for an entire site so I have to go through a separate file which I will post the code from later):
4
Mast
4
Mast
{"success":"noerror"}
Here is the way I have to check for errors:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
include("register.php");
?>
And here is the function which calls the function in question above:
function register($username, $password, $email, $squestion, $sanswer)
{
//First check if the email is valid
$data = eVerify($email);
//If email is not valid
if (!$data)
{
return "email_fail";
}
//If email is valid then check if it already exists and the verification status
else
{
//See if the email already exists
$data = getUID($email,"email",true);
//echo $data."\n";
if ($data)
{
//Get user ID for later use
$id = getUID($email,"email",false);
//If the email exists, see if it has been verified or not
$data = checkVer($id);
//echo $data."\n";
//If email exists but has not been verified
if (!$data)
{
rSVCode($username,$email,$id);
return "exists1";
exit();
}
//If email exists and has been verified
else if ($data)
{
return "exists2";
exit();
}
}
//If email does not exist, continue registration process
else
{
//Check to see if username has been used
$data = getUID($username,"username",true);
if ($data)
{
return "un_exists";
exit();
}
//Check password strength, chars, and length
else
{
$data = checkPassword($password);
if ($data)
{
//Create user account
$data = cAccount($username, $password, $email, $squestion, $sanswer);
if ($data)
{
//Get user's ID for use later
$id = getUID($username,"username",false);
//Generate email verification code
$data = cVCode($username,$email,$id);
//Send verification email
$data = sendEVar($email,$username,$data);
if ($data)
{
return "true";
exit();
}
else
{
return $data;
exit();
}
}
else
{
return $data;
exit();
}
}
else
{
return $data;
exit();
}
}
}
}
}
The triple === makes sure the return is of the same type.
In your function you don't always return boolean, sometimes you return strings, and that could be an issue.
For example this snippet:
$data = "pass_charfail";
if($data){
echo 'true';
}else{
echo 'false';
}
this will echo true because $data is not an empty string.
But the following will echo false, because $data is not a true boolean.
$data = "pass_charfail";
if($data === true){
echo 'true';
}else{
echo 'false';
}
One more example in your register function you have
if ($data)
{
return "true";
exit();
}
if this value gets return, then false will be echo from the following code:
if($data === true){
echo 'true';
}else{
echo 'false';
}
because $data is now a string which is not of type boolean.
hope it makes sense to you!
I got it working again but I am not sure why the change I made makes a difference. If someone could respond to this answer or post their own answer explaining it would be appreciated.
How I fixed it was changing the if ($data) line after checkPassword is called to if ($data === true) and it reported the correct error message instead of claiming a successful registration.

Categories