mt_rand() returns unexpected output - php

I am working with my ad system of my site and having trouble with this.
<?php
function bdads($size, $company) {
if($company == 'nufa') {
if ($size == '300'){
echo 'n300';
}
if ($size == '160'){
echo 'n160';
}
if ($size == '728'){
echo 'n728';
}
if ($size == '700'){
echo 'n700';
}
}
if($company == 'gnr') {
if ($size == '300'){
echo 'g300';
}
if ($size == '160'){
echo 'g160';
}
if ($size == '728'){
echo 'g728';
}
if ($size == '700'){
echo 'g700';
}
}
}
function bdad($size, $company){
$zsize = $size;
if($company == 'nufa'){
echo bdads($zsize, 'nufa');
}
if($company == 'gnr'){
echo bdads($zsize, 'gnr');
}
if($company == 'both'){
$RandomList = [ bdads($zsize, 'gnr'), bdads($zsize, 'nufa')];
echo $RandomList[mt_rand(0, count($RandomList) - 1)];
}
}
?>
Now, Everything seems fine.. as example,
<?php echo bdad(728, 'gnr'); ?>
returning g728 (as expected)
<?php echo bdad(300, 'nufa'); ?>
returning n300 (as expected)
But all trouble is in generating random content.
<?php echo bdad(300, 'both'); ?>
returning g300n300
I want it to choose either g300 or n300 randomly.
EDIT:
Changed $a to $RandomList, but still same result

I tried to simplify your functions a bit:
You have to return your values, otherwise your functions will return NULL by default. Also you can access a string like an array, so I used $company[0] to get the first letter of the company, which you then can concatenate with the size.
<?php
function bdads($size, $company) {
return $company[0] . $size;
}
function bdad($size, $company){
if($company == "both") {
$RandomList = [bdads($size, "gnr"), bdads($size, "nufa")];
return $RandomList[mt_rand(0, count($RandomList) - 1)];
} else {
return dads($size, $company);
}
}
echo bdad(300, "both");
?>
output:
n300 //Or g300

Related

How can I define a variable inside a function?

how I can define a variable inside a PHP function and make it reachable from another function? I want to set $error variable to "1".
This how would the code look like:
function checkthename ($name){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
function phone ($phone){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
etc.
And in end of the code there would be an another function which is checking if there is any $error variable set to 1
function checktheerror($error){
if ($error == 0){
echo "no error in this code";
}
elseif ($error == 1){
echo "there is one or multiple errors";
}
}
Thank you so much for your help!

Read user input and check data type

I've simple PHP script:
<?php
$input = readline();
echo gettype($input);
?>
It reads user input from the console. What I am trying to achieve is to get properly data type. At the moment $input is string type.
I need something like this:
Input Output
5 Integer
2.5 float
true Boolean
I can't get any idea how to do it. Thanks.
EDIT: Thanks to #bcperth answer, I achieve this working code:
<?php
while(true) {
$input = readline();
if($input == "END") return ;
if(is_numeric($input)) {
$sum = 0;
$sum += $input;
switch(gettype($sum)) {
case "integer": $type = "integer"; break;
case "double": $type = "floating point"; break;
}
echo "$input is $type type" . PHP_EOL;
}
if(strlen($input) == 1 && !is_numeric($input)) {
echo "$input is character type" . PHP_EOL;
} else if(strlen($input) > 1 && !is_numeric($input) && strtolower($input) != "true" && strtolower($input) != "false") {
echo "$input is string type" . PHP_EOL;
} if(strtolower($input) == "true" || strtolower($input) == "false") {
echo "$input is boolean type" . PHP_EOL;
}
}
?>
Also tried with filter_var, working well:
<?php
while(true) {
$input = readline();
if($input == "END") return;
if(!empty($input)) {
if(filter_var($input, FILTER_VALIDATE_INT) || filter_var($input, FILTER_VALIDATE_INT) === 0) {
echo "$input is integer type" . PHP_EOL;
} else if(filter_var($input, FILTER_VALIDATE_FLOAT) || filter_var($input, FILTER_VALIDATE_FLOAT) === 0.0) {
echo "$input is floating point type" . PHP_EOL;
} else if(filter_var($input, FILTER_VALIDATE_BOOLEAN) || strtolower($input) == "false") {
echo "$input is boolean type" . PHP_EOL;
} else if(strlen($input) == 1) {
echo "$input is character type" . PHP_EOL;
} else {
echo "$input is string type" . PHP_EOL;
}
}
}
?>
You would need to employ a few strategies as below for simple types.
test if numeric using is_numeric().
if numeric then add it to zero and gettype() the result
if not numeric then compare to "true" and "false"
if not "true" or "false" then its a string
Here is a working start that shows how to go about it.
<?php
$input = readline();
if (is_numeric($input)){
$sum =0;
$sum += $input;
echo gettype($sum);
}
else {
if ($input== "true" or $input == "false"){
echo "boolean";
}
else {
echo "string";
}
}
?>

PHP empty variable inside condition

I have a condition in which it is using a variable to pull either a number through 0-17, the string "MAKEUP", or the variable will be empty. I would like it to output the text "WIN" if the variable is greater than the number 8 and "LOSS" if the variable is less than the number 9. I would also like it to out "MAKEUP" if the variable consist of the string MAKEUP, and to display nothing if the variable is empty. Seems pretty simple to me, but I'm having issues particularly with the empty part. Can anyone let me know what I am doing wrong here? Code below
<?php
$t1w8 = '';
$result = $t1w8;
if ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result < 9 && !empty($result)) {
echo 'LOSS';
} elseif ($result == 'MAKEUP') {
echo '-';
} else {
echo 'yooo';
}
?>
Make some changes in your conditions like this
<?php
//$result = "MAKEUP";
$result = 0;
if ($result === 'MAKEUP') {
echo '-';
}else if (is_numeric($result) && $result < 9 ) {
echo 'LOSS';
}else if (is_numeric($result) && $result >= 9 ) {
echo 'WON';
} else{
echo 'yooo';
}
?>
Live demo : https://eval.in/897120
try with this code
<?php
//$result = "MAKEUP";
$result = "";
//$result = "9";
//$result = "-";
if ($result == 'MAKEUP' && !empty($result) ) {
echo '-';
} elseif ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result <= 8 && !empty($result)) {
echo 'LOSS';
} else {
echo 'yooo';
}
?>
for demo :demo code here
You have explained that your number range is from 0-17.
You have also explained that you could get the word MAKEUP.
Based upon those constraints we could use something like this
$output = "";
// Do we have something?
if(strlen($result) > 0) {
if (strtolower($result) == "makeup") {
$output = "MAKEUP";
}
// assumes a single digit string
else if ($result < 9) {
$output = "LOSS";
} else if ($result <= 17) {
$output = "WIN";
}
}
echo $output;

big IF function not working in PHP?

Dears,
I am surprised why the PHP 'IF ELSE' function is not working properly. I guess it's a bit long but I made Algorithm for it and logically it would work perfectly. Can anyone have any clue why the function not working properly . please ..
<?php
//entry Marks
$tc=80;$tf=33;$pc=null;$pf=50;
if($tc!=NULL && $tf!=NULL && $pc!=NULL && $pf!=NULL){
echo $tc." ".$tf." ".$pc." ".$pf;
}else{
if($tc!=NULL){
if($tf!=NULL && $pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($tf!=NULL){
if($pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "tf.pc.pf";
}else{
echo "tf.pc";
}
}else{
if($pf!=NULL){
echo "tf.pf";
}else{
echo "tf";
}
}
}
}else{
if($pc!==NULL && $pf!=NULL){
echo "pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "pc.pf";
}else{
echo "pc";
}
}else{
if($pf!=NULL){
echo "pf";
}else{
echo "null";
}
}
}
}
}
}else{ //2nd part
if($tf!=NULL && $pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($tf!=NULL){
if($pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "tf.pc.pf";
}else{
echo "tf.pc";
}
}else{
echo "tf";
}
}
}else{
if($pc!=NULL && $pf!=NULL){
echo "pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "pc.pf";
}else{
echo "pc";
}
}else{
if($pf!=NULL){
echo "pf";
}else{
echo "null";
}
}
}
}
}
}
}
?>
Your code is really difficult to follow, this code sample below shows a simplified version which appends to a string the values or text depending on if the variable is NULL or not. You can change what the if-else statements add to the result string easily. At the end I remove the "." at the end of the final string.
$result = "";
if($tc == NULL){
$result .= "tc.";
}else{
$result .= $tc.".";
}
if($tf == NULL){
$result .= "tf.";
}else{
$result .= $tf.".";
}
if($pc == NULL){
$result .= "pc.";
}else{
$result .= $pc.".";
}
if($pf == NULL){
$result .= "pf.";
}else{
$result .= $pf.".";
}
$output = rtrim($result, '.');
echo $output;
I will update my answer when its a bit clearer what output you are after
As noted in the other answer, your code is much longer than needed. This length makes it hard to follow. You could use a foreach loop to go through all your columns (which will make adding columns much easier).
Assuming I understand what you are looking for, you could use something like:
$tc=80;$tf=33;$pc=null;$pf=50;
$columns = array('tc'=>$tc,'tf'=>$tf,'pc'=>$pc,'pf'=>$pf);
$i = 0;
$output = "";
foreach ($columns as $key=> $column) {
if (!$column == null) {
$output .= $key.".";
}
}
// strip last period:
$output = rtrim($output, '.');
echo $output;
Link to php sandbox for demo here.
I do not have the answer of why your large if-statement do not work. But I do have a suggestion of making a PHP function to handle your specific need:
function check($tc=null,$tf=null,$pc=null,$pf=null){
$r = array();
if($tc != null){ $r[] = 'tc'; }
if($tf != null){ $r[] = 'tf'; }
if($pc != null){ $r[] = 'pc'; }
if($pf != null){ $r[] = 'pf'; }
if(empty($r)){
return 'null';
} else {
return implode('.', $r);// MAKE STRING OF ARRAY
}
}
And call the function like this:
$tc=80;$tf=33;$pc=null;$pf=50;
echo check($tc, $tf, $pc, $pf);
Try it out: https://eval.in/735202

Several nearly identical if statements need to be shortened

So I've got this code...
code:
if(empty($day0[2])) {
echo "<td>".$day0[1]."<br></td>";
} else {
if(strcmp($day0[1],"Absent") == 0) {
echo "<td>".$day0[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day0[1]."<br>Time: ".#$day0[2]." - ".#$day0[3]."</td>";
}
}
if(empty($day1[2])) {
echo "<td>".$day1[1]."<br></td>";
} else {
if(strcmp($day1[1],"Absent") == 0) {
echo "<td>".$day1[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day1[1]."<br>Time: ".#$day1[2]." - ".#$day1[3]."</td>";
}
}
if(empty($day2[2])) {
echo "<td>".$day2[1]."<br></td>";
} else {
if(strcmp($day2[1],"Absent") == 0) {
echo "<td>".$day2[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day2[1]."<br>Time: ".#$day2[2]." - ".#$day2[3]."</td>";
}
}
if(empty($day3[2])) {
echo "<td>".$day3[1]."<br></td>";
} else {
if(strcmp($day3[1],"Absent") == 0) {
echo "<td>".$day3[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day3[1]."<br>Time: ".#$day3[2]." - ".#$day3[3]."</td>";
}
}
if(empty($day4[2])) {
echo "<td>".$day4[1]."<br></td>";
} else {
if(strcmp($day4[1],"Absent") == 0) {
echo "<td>".$day4[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day4[1]."<br>Time: ".#$day4[2]." - ".#$day4[3]."</td>";
}
}
if(empty($day5[2])) {
echo "<td>".$day5[1]."<br></td>";
} else {
if(strcmp($day5[1],"Absent") == 0) {
echo "<td>".$day5[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day5[1]."<br>Time: ".#$day5[2]." - ".#$day5[3]."</td>";
}
}
if(empty($day6[2])) {
echo "<td>".$day6[1]."<br></td>";
} else {
if(strcmp($day6[1],"Absent") == 0) {
echo "<td>".$day6[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day6[1]."<br>Time: ".#$day6[2]." - ".#$day6[3]."</td>";
}
}
Where day 0-6 equals Sunday through Saturday, and the array numbers attached to each one equals a different variable in a multi-dim array.
That is several if statements that are all exactly the same except the variable name inside of each one. I haven't been able to find a way to make this shorter, so I thought I would post here to try and see if anyone has any ideas on how I can combine this into shorter lines of code. I'm all about my code looking neater and functioning better, and I think this could teach a lot of people good ways to shorten their code down a bit.
First merge all array of $day[n] in to $finalArray
foreach($finalArray as $key=>$value){
if(empty($value[2])) {
echo "<td>".$value[1]."<br></td>";
} else {
if(strcmp($value[1],"Absent") == 0) {
echo "<td>".$value[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$value[1]."<br>Time: ".#$value[2]." - ".#$value[3]."</td>";
}
}
}
#MagnusEriksson suggested making a function, I think this is the best way to do it.
From 69 lines of code to 18 lines of code.
function displayTime($day1,$day2,$day3) {
if(empty($day2)) {
return "<td>{$day1}<br></td>";
} else {
if(strcmp($day1,"Absent") == 0) {
return "<td>{$day1}<br>Time: N/A</td>";
}
return "<td>{$day1}<br>Time: {$day2} - {$day3}</td>";
}
}
for ($x = 0; $x <= 6; $x++) {
echo displayTime(${"day$x"}[1],${"day$x"}[2],${"day$x"}[3]);
}
Please try with this code.
$array=array($day0,$day1,$day2,$day3);
for($i=0;$i<count($array);$i++){
if(empty($day.$i[2])) {
echo "<td>".$day.$i[1]."<br></td>";
} else {
if(strcmp($day.$i[1],"Absent") == 0) {
echo "<td>".$day.$i[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day.$i[1]."<br>Time: ".#$day.$i[2]." - ".#$day.$i[3]."</td>";
}
}
}

Categories