php if return == false echo something - php

I'm using a include file to translate my text. it works pretty good, but now I need to on button click translate some mor words, and return doesn't work anymore, but echo does.
so what I'm searching is a way of know if return is possible or not, code example
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
return $traducao_1[$i];
}
if($lingua == 2) {
return $traducao_2[$i];
}
}
}
this one works good first time page is executed, since this is included file.
how to make this?
if(!return $traducao_1[$i]) {
thanks
ok, tryed to answer, but always got an error, so I'm editing this as answer
Thank you all for help, I manage a way of make it work like adding one action to the function and checking if action == , then do something, like this
function test($palavra, $lingua, $accao) {
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
if($accao != "2_chamada") {
return $traducao_1[$i];
} else {
echo $traducao_1[$i];
}
}
}
}
}
Again, thanks for help

maybe check to see if it's set first before returning
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
$return = $traducao_1[$i];
if($return != false) {
return $return;
} else {
echo 'something';
}
}
if($lingua == 2) {
$return = $traducao_2[$i];
if($return != '') {
return $return;
}
}
}
}

return can only be used in functions
i reccommend this for you
function test($palavras,$palavra,$traducao_1,$traducao_2){
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
return $traducao_1[$i];
}
if($lingua == 2) {
return $traducao_2[$i];
}
}
}
}
if(!test($palavras)) {

Related

Run function when if statement is true (For Loop)

I have one issue that I can't solve. So I have for loop.
So here is little code:
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($sms->mobio_check($servID,$request->input("code$int"))) {
continue;
$cart->success($product->id,$product->server->name);
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
I want if three ifs return true to run function $sms->success();.
What is wrong here?
You could rely on the fact that if the loop finished, then it's OK, any failures will cause the return in the loop to exit...
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if( ! $sms->mobio_check($servID,$request->input("code$int"))) {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
$cart->success($product->id,$product->server->name);
You can do it like this:
$success = true;
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($success = $success || $sms->mobio_check($servID,$request->input("code$int"))) {
continue;
$cart->success($product->id,$product->server->name);
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
if ($success) $sms->success();
you can try this
$count = 0;
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($sms->mobio_check($servID,$request->input("code$int"))) {
$count++;
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
if($count == 3)
$cart->success($product->id,$product->server->name);

How to do process with for() function and give return true in the last

I have a project, and I have one problem. Can you help me? I have this code:
for($i=0; $i < $rows; $i++){
if(do_something == true){
return true;
}else{
return false;
}
}
If, I have 10 data, and I want do_something is process all my data. But the problem is, the script is stop in first data. So, I delete the return true; and leave it blank. Now the script process all my data. My question is, how can I put return true; in the end of the process, so I know no error in the process.
use this:
for($i=0; $i < $rows; $i++){
if(do_something == false){
return false;
}
}
return true;
it will "do something" as long as it is successful, and until all are done, and abort at the first "false" result of "do something".
Just change your logic a bit and do something like this:
So if something goes wrong you stop and return FALSE, if everything went correct you end the loop and return TRUE.
for($i = 0; $i < $rows; $i++) {
if(do_something != TRUE)
return FALSE;
}
return TRUE;
you can try this
$j=0;
for($i=0; $i < $rows; $i++) {
if(do_something == true) {
/*return true;*/
$j++;
} else {
return false;
}
}
if($j == $rows) {
return true;
} else {
return false;
}

PHP timeout error handling

I'm triyng to catch timeout error to output some clear text to the user (like "Sorry, timeout").
So why does this example:
function shutdown() {
$a=error_get_last();
if($a==null)
echo "No errors";
else
print_r($a);
}
register_shutdown_function('shutdown');
ini_set('max_execution_time',1 );
sleep(3);
output no errors?? I'm confused about it.
Here this example looks helpful.
Thanks
Try not using sleep(), seems to work if the reason for timeout is real work:
Example
function isPrime($num) {
if($num == 1)
return false;
if($num == 2)
return true;
if($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
function shutdown()
{
$a=error_get_last();
if($a==null)
echo "No errors";
else
print_r($a);
}
register_shutdown_function('shutdown');
ini_set('max_execution_time',1 );
$ps = 0;
for ($i = 0; $i < 1000000; $i++) {
if (isPrime($i)){
$ps++;
}
}
echo $ps;

Check Prime Number In PHP

My html document has the following form:
<form action="PrimeNumber.php" method="post">
Enter a number to determine if it is a prime number: <input type="text" name="numb" size="10">
<input type="submit" value="Check for Primeness">
</form>
Edit: Using a different code now but can't get it to echo a statement no matter what I do. Anyone know how I can make it echo is a prime number or is not.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['numb'])) {
$num = $_POST['numb'];
function isPrime($num) {
if($num == 1) {
return false;
}
if($num == 2) {
return true;
}
if($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
}
}
?>
simple and easy to understand code, and working as well. do little changes according to your requirement, like assign input text value to $a variable and you good to go.
$a = 51;
for($i=2; $i < $a; $i++){
if($a % $i == 0){
echo "Numberis not prime.";
break;
}
else{
echo "Number is not prime.";
break;
}
}
A simple function to check is prime number:
function is_prime($p) {
return ($p > 1) && (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) || in_array($p, [2,3,5]);
}
function checkPrime($num){
$isPrime = true;
for($i = 2; $i <= sqrt($num); $i++)
{
if($num % $i == 0)
{
$isPrime = false;
}
}
if($isPrime == true)
{
return "$num is prime number";
}else{
return "$num is not prime number";
}
}
echo checkPrime(29);
The output is
29 is prime number
For More Details
You may use the gmp package and correct your code because I got an output as 11 not being a prime number or use the following function as an alternative.
function isPrime($num) {
if($num == 1) {
return false;
}
if($num == 2) {
return true;
}
if($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
More information is available at the following page.
A simple function to help you find out if a number is a prime number
<?php
function fn_prime($number) {
$i = 2; $result = TRUE;
while($i < $number) {
if(!($number%$i)) {
$result = FALSE;
}
$i++;
}
return $result;
}
?>

What is wrong with my code - circularly sorted array does not show any results

I had an interview today and the person asked me this question:
How do you find easily an item in a circularly sorted array
Since I didn't know the answer, I tried to find a solution. Here's what I have:
Thanks
<?php
function searchincircularsorterlist($a, $len, $num) {
$start=0;
$end=$len-1;
$mid = 0;
while($start<$end) {
$mid=$start+$end/2;
if ($num == $a[$mid]) {
return $num;
}
if($num<$a[$mid]) {
if($num<$a[$start] && $a[$start]<=$a[$start+1])
$start=$mid++;
else
$end=$mid--;
}
else {
if($num>$a[$end] && $a[$end-1]<=$a[end])
$end=$mid--;
else
$start=$mid++;
}
}
if ($start == $end && $num == $a[$start]) {
return $num;
}
return -1;
}
$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));
I am trying to work with a circularly sorted array but for some reason it does not work. What's wrong with my code?
1) learn priority of operations. You should have: $mid=($start+$end)/2; which you ended up dividing $end by 2 and then $start - the result. This is why you got an infinite loop.
2) use: $start=$mid+1; and not $start=$mid++; that will help reducing the number of loops
<?php
function searchincircularsorterlist($a, $len, $num) {
$start=0;
$end=$len-1;
$mid = 0;
while($start<$end) {
$mid=($start+$end)/2;
if ($num == $a[$mid]) {
return $num;
}
if($num<$a[$mid]) {
if($num<$a[$start] && $a[$start]<=$a[$start+1])
$start=$mid+1;
else
$end=$mid-1;
}
else {
if($num>$a[$end] && $a[$end-1]<=$a[end])
$end=$mid-1;
else
$start=$mid+1;
}
}
if ($start == $end && $num == $a[$start]) {
return $num;
}
return -1;
}
$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));

Categories