defining alphabets as numbers not working inside loop - php

Please check my code below,it returns 0 while I am expecting a result 14.But when I add A+D manually it returns 5.Am i doing something wrong inside the loop ?
<?php
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
for($i = 0; $i<=$len; $i++)
{
$val += $name[$i];
}
echo $val; //returns 0
?>

You need to use constant(..) to get the value of a constant by name. Try this:
for ($i = 0; $i < strlen($name); $i++) {
$val += constant($name[$i]);
}

define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
$val = null;
for($i = 0; $i<=$len-1; $i++)
{
$val += constant($name[$i]);
}
echo $val;

Related

Why can't I use a function to simplify my "for loops"

I build a webpage to crack simple MD5 hash of a four digit pin for fun. The method I used was basically try all combination and check against the MD5 value the user has entered. Below is the PHP code I created to accomplish the goal.
Debug Output:
<?php
$answer = "PIN not found";
if (isset($_GET['md5'])) {
$txt = 'abcdefjhig';
$time_pre = microtime(TRUE);
$value = $_GET['md5'];
$show = 15;
for ($i = 0; $i <= 9; $i++) {
$first = $i;
for ($j = 0; $j <= 9; $j++) {
$second = $j;
for ($k = 0; $k <= 9; $k++) {
$third = $k;
for ($x = 0; $x <= 9; $x++) {
$fourth = $x;
$whole = $first . $second . $third . $fourth;
$check = hash('md5', $whole);
if ($check == $value) {
$answer = $whole;
echo "The pin is $answer";
}
if ($show > 0) {
print"$check $whole \n";
$show = $show - 1;
}
}
}
}
}
echo "\n";
$time_post = microtime(TRUE);
print "Elapsed time:";
print $time_post - $time_pre;
}
?>
Notice that in the middle there are four very similar for loops,I tried to simplify this by using functions, but it just returns one four digit number which is 9999 instead of all of them.
Below is the function I created:
function construct($input){
for($i=0; $i<=9, $i++){
$input=$i;
}
return $input
}
Then I tried to call this function for four times to form all of the four digit numbers but it just gives me 9999. Can anybody help? Thanks a lot.
Try this:
for ($i=0; $i<=9999 ; $i++) {
$whole = sprintf('%04d', $i);
$check=hash('md5', $whole);
if($check==$value){
$answer=$whole;
echo "The pin is $answer";
break; //remove this if you do not want to break the loop here
}
if ($show>0) {
print"$check $whole \n";
$show=$show-1;
}
}
You're using numbers from 0 to 9999, so why just loop through those numbers? You can add zero's by using str_pad() like so:
for ($i = 0; $i <= 9999; $i++) {
$whole = str_pad($i, 4, '0', STR_PAD_LEFT);
}
Also I'd like to mention that you really should think about better formatting of your code, especially indentation

Multiple comparisons inside for loops don't break php code. Why?

Why this piece of code works when it is clearly wrong in the second for loop (for ($i==0; $i<$parts; $i++) {)?
Does php allows for multiple comparisons inside for loops?
function split_integer ($num,$parts) {
$value = 0;
$i = 0;
$result = [];
$modulus = $num%$parts;
if ($modulus == 0) {
for($i = 0; $i < $parts; $i++)
{
$value = $num/$parts;
$result[] = $value;
}
} else {
$valueMod = $parts - ($num % $parts);
$value = $num/$parts;
for ($i==0; $i<$parts; $i++) {
if ($i >= $valueMod) {
$result[] = floor($value+1);
} else {
$result[] = floor($value);
}
}
}
return $result;
}
Code for ($i==0; $i < $parts; $i++) runs because $i==0 has no impact on loop.
In normal for loop first statement just sets $i or any other counter's initial value. As you already set $i to 0 earlier, your loop runs from $i = 0 until second statement $i < $parts is not true.
Going further, you can even omit first statement:
$i = 0;
for (; $i < 3; $i++) {
echo $i;
}
And loop will still run 3 times from 0 to 2.

Get max/min value from json file with PHP

Im am trying to get the min and max temp value from a json src. I'm not getting the desired result that I am looking for. This is what i have so far.
Any help is greatly appreciated
<?php
$url = 'https://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b6907d289e10d714a6e88b30761fae22';
$data = file_get_contents($url);
$forecasts = json_decode($data);
$length = count($forecasts->list);
for ($i = 0; $i < $length; $i++) {
$val = round($forecasts->list[$i]->main->temp, 0);
}
$min = min($val);
$max = max($val);
echo "max: ". $max ." - min: ". $max;
?>
You are overwriting the value of $val in each pass through your for loop. What you actually want to do is push each value into an array that you can then take the min and max of:
$val = array();
for ($i = 0; $i < $length; $i++) {
$val[] = round($forecasts->list[$i]->main->temp, 0);
}
$min = min($val);
$max = max($val);
Ok so I'm assuming that "temp" is the attribute where you want to get min/max:
<?php
function max($list){
$highest = $list[0]->main->temp;
foreach($list as $l){
$highest = $l->main->temp > $highest ? $l->main->temp : $highest;
}
return $highest;
}
function min($list){
$lowest = $list[0]->main->temp;
foreach($list as $l){
$lowest = $l->main->temp < $lowest ? $l->main->temp : $highest;
}
return $lowest;
}
$url = 'https://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b6907d289e10d714a6e88b30761fae22';
$data = file_get_contents($url);
$forecasts = json_decode($data);
$length = count($forecasts->list);
$min = min($forecasts->list);
$max = max($forecasts->list);
echo "max: ". $max ." - min: ". $max;
?>
should be copy-pastable for your case...

counting the number of items in an array

I have a string as shown bellow:
SIM types:Azadi|Validity:2 Nights|Expirable:yes
I have the following code to seperate them by | and then show them line by line
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others['items'][] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
echo $others['items'][$i];
}
but the for loop is iterated only once and prints only the first value. This is what i am getting now:
SIM types:Azadi
Try like this
$others['items'] = explode("|",$other);
$my_count = count($others['items']);
for($i = 0; $i < $my_count; $i++){
echo $others['items'][$i];
}
Change
$others['items'][] = explode("|",$other);
to
$others['items'] = explode("|",$other);
remove []
Explode will return a array. ref: http://php.net/manual/en/function.explode.php
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others['items'] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
echo $others['items'][$i];
}
Try this:
<?php
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others = explode("|",$other);
$total = count($others);
for($i = 0; $i < $total; $i++){
echo $others[$i];
}
?>

add in loop in multi dimensional array

i am facing a problem
can some one suggest me
for ($i = 1; $i <= 2; $i++) {
$r2 = 0;
for ($t = 1; $t <= 2; $t++) {
echo $r2;
$r2++
}
}
output is 0101;
can i get output 0123 ??? please
if
for ($i = 1; $i <= 3; $i++) {
$r2 = 0;
for ($t = 1; $t <= 3; $t++) {
echo $r2;
$r2++
}
}
output is 010101;
can output 012345678 ??? please
and if
for ($i = 1; $i <= 4; $i++) {
$r2 = 0;
for ($t = 1; $t <= 4; $t++) {
echo $r2;
$r2++
}
}
output is 01010101;
can output 0123456789101112131415 ??? please
i think you understand
thanks
In all of these cases you are initializing $r2=0; in the inner loop. It should be outside the loop.
$r2=0;
for($i=1;$i<=2;$i++){
for($t=1;$t<=2;$t++){
echo $r2;
$r2++
}
}
This would produce "1234".
why are you using two nested for loops ?
why not just use one:
for ($i=0; $i<=15; $i++) echo $i . " ";
Try this:
$r2 = 10;
for($t = 0; $t <= $r2; $t++){
echo $r2;
}
Oh wait.. I get it now, why you have the two nested loops, you want to essentially raise a number to the power of 2 in order to control the number of values output. In that case, what you want is simply this:
// this is the variable you need to change to affect the number of values outputed
$n = 2;
// Square $n
$m = $n * $n;
// Loop $m times
for ($i = 0; $i < $m; $i++) {
echo $i;
}

Categories