I have this code:
if ( ($oldTime < (time() - self::wait)) ) {
if ($this->setTime())
{
return true;
}
else
{
return false;
}
} else {
return false;
}
Can i replace it with:
if ( ($oTime < (time() - self::wait)) && $this->setTime() ) {
return true;
} else {
return false;
}
I need it to check if $this->setTime() returns true ONLY if $oTime < (time() - self::wait) is true.
return ($oTime < (time() - self::wait)) && $this->setTime()
yes you can use this
if ( ($oTime < (time() - self::wait)) && $this->setTime() ) {
return true;
} else {
return false;
}
if first condition in the if statement with && ( not || ) fails, it will go to the else branch automatically without verifying the second condition
Related
I am setting some vars from GET
$start = $_GET['start'];
$end = $_GET['end'];
From which I get:
start=1-11-2018&end=30-11-2018
And then I am doing:
if((!$start) && (!$end)) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
echo "no dates";
}
And to close it
if((!$start) && (!$end)) {
}
}
But this isn't happening
if((!$start) && (!$end)) {
UPDATE
Now this is working but it doesn't go in else if no GET
if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
echo "No dates";
}
Check via isset()
if you are calling : http://example.com?start=1-11-2018&end=30-11-2018
1. The isset() is checking query string "start"/"end" is having or not.
2. The empty() is checking query string "start"/"end" is empty/blank or not
if( isset($_GET['start']) && isset($_GET['end']) ){ // check the GET method is set or not
if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
}
else {
echo "Empty dates";
}
}
else{
echo "Start / End date query string is missing...";
}
This is how I resolved it:
if((!empty($start)) && (!empty($end))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
}
// here the content which
// in case those vars are not empty,
// would get filtered by that logic.
if((empty($start)) && (empty($end))) {
// Close the condition for second if when not empty
} else {
echo "No dates";
}
Make a function has_twenty_ones that returns true if at least one of the players in the game has 21, otherwise return false. This function should use the twenty_ones function.
function has_twenty_ones($game){
function twenty_ones($game)
{
$players_with_score_21 = [];
foreach ($game['players'] as $name => $player) {
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
if ($distance == 21) {
$players_with_score_21 = [$name];
}
}
return $players_with_score_21;
}
return isset($players_with_score_21);
}
what's the best way to code it
Just check if return of twenty_ones function is empty, if it return false overthose return twenty_ones value.
function has_twenty_ones($game){
function twenty_ones($game){
$players_with_score_21 = [];
foreach ($game['players'] as $name => $player) {
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
if ($distance == 21) {
$players_with_score_21 = [$name];
}
}
return $players_with_score_21;
}
$playersWithScore = twenty_ones($game);
if (!empty($playersWithScore)) {
return $playersWithScore;
} else {
return false;
}
}
I'm not sure why you need two functions for this.
As was mentioned by #RiggsFolly, you're not actually making a call to twenty_ones() function. Why not have the following code:
function has_twenty_ones($game)
{
foreach($game['players'] as $name => $player)
{
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
// If at least one player has 21, return true.
if($distance == 21) {
return true;
}
}
return false;
}
The above will return true when it encounters a player who has a score of 21, otherwise it'll return false.
function twenty_ones($game)
{
$players_with_score_21 = [];
foreach ($game['players'] as $name => $player) {
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
if ($distance == 21) {
$players_with_score_21 = [$name];
}
}
return $players_with_score_21;
}
function has_twenty_ones($game){
if (count ($this->twenty_ones($game)) > 0 )
return true;
else
return false;
}
I have to write the function to check validation of input number from browser:
-> is numeric
-> has 6 letters
-> in form: odd+even+odd+even,... the 1st character can be odd or even.
For example: 123456 => true; 234567 => true
I wrote:
function check_code($code){
if (!is_numeric($code)) return false;
if (strlen($code)<>6) return false;
$c = str_split($code);
if (($c[0]+$c[1])%2==1 && ($c[1]+$c[2])%2==1 && ($c[2]+$c[3])%2==1 && ($c[3]+$c[4])%2==1 && ($c[4]+$c[5])%2==1) return true;
return false;
}
Is there any other solution "shorter and smarter" than above code? Thank you
You could consider rewriting the logic using the modulus operator into a for loop:
function check_code($code)
{
if (!is_numeric($code)) return false;
if (strlen($code)<>6) return false;
$c = str_split($code);
for ($i = 0; $i < count($c); $i++) {
if (isset($c[$i + 1])) {
if (($c[$i] + $c[$i + 1]) % 2 !== 1) {
return false;
}
}
}
return true;
}
I'm trying to check if a date is real, it must return true, and if not, it must return false.
It does not seemes to work when I write 35-02-2012 (Date format dd-mm-yy) it return true, but I was expecting false, I do not know where I'm wrong.
below is my function
function isItRealDate($date) {
if ($date == '') {
return false;
} else {
$rxDatePattern = '/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/'; //Declare Regex
$dtArray = preg_match($rxDatePattern, $date); // is format OK?
if ($dtArray == '0') {
return false;
} else {
$tableau_date = explode('-', $date);
//Checks for dd-mm-yyyy format.
$dtMonth = $tableau_date[1];
$dtDay = $tableau_date[0];
$dtYear = $tableau_date[2];
if ($dtMonth < 1 || $dtMonth > 12) {
return false;
} elseif ($dtDay < 1 || $dtDay > 31) {
return false;
} elseif (($dtMonth == 4 || $dtMonth == 6 || $dtMonth == 9 || $dtMonth == 11) && $dtDay == 31) {
return false;
} elseif ($dtMonth == 2) {
$isleap = ($dtYear % 4 == 0 && ($dtYear % 100 != 0 || $dtYear % 400 == 0));
if ($dtDay > 29 || ($dtDay == 29 && !$isleap)) {
return false;
} else {
return true;
}
}
}
}
}
anykind of help will be much appreciated
If you want something that just works, use checkdate().
As suggested by Ibrahim, use:
function isItRealDate($date) {
if(preg_match("#^(\d{1,2})\-(\d{1,2})\-(\d{1,4})$#", $date, $match)){
//regex could match 99-99-9999 - checkdate will take care.
return checkdate($match[2], $match[1], $match[3]);
}
return false;
}
This will work for ANY (valid) date between 1-1-1 and 31-12-9999
how can I check for numbers only from -10 negative to +10 positive?
This is what I have, but I think it's not safe:
if(isset($_POST['number']) && ctype_digit($_POST['number']) && $_POST['number']>=-10 && $_POST['number']<=10){
//do something
}
and the form:
Input a number between -10 and 10: <input type="text" name="number" size="5" />
if( isset($_POST['number'])) {
$num = intval($_POST['number']);
if( $num >= -10 && $num <= 10) {
// do something
}
}
There are other ways, but that one will work. Anything that can't be converted to a number will be treated as zero. If this is not desired behaviour, add:
&& "".$num == $_POST['number']
To that inner IF statement, to ensure that no non-numeric characters were removed from the input.
Check whether a variable is a number including zero and negative values
$x = '-22';
if (isNumber($x, ['zero','negative']))
echo 'Yes';
else
echo 'No';
isNumber($x, $includes=[])
{
if (is_int($x)) {
if ($x === 0) {
if (in_array('zero', $includes))
return true;
} elseif ($x < 0) {
if (in_array('negative', $includes))
return true;
} else
return true;
} elseif (is_string($x)) {
if ($x == '0') {
if (in_array('zero', $includes))
return true;
} elseif ($x[0] == '-') {
if (in_array('negative', $includes))
return ctype_digit(substr($x, 1));
} else
return ctype_digit($x);
}
}