PHP: Basic loop [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I recently started to try to teach myself PHP. I've only taken a beginner class in C before, so this is a little new to me.
I was fiddling around with some basic code just to practice:
<?php
$num = 0;
while ($num < 5)
{
if ($num == 1)
{
echo 'There is' . $num . ' monkey.';
}
else
{
echo 'There are ' . $num . ' monkeys.';
$num++;
}
}
?>
However, it won't run and Chrome asks me if I would like to kill the page.
Did I create an infinite loop somehow without realizing it?
Thank you!

You did create an infinite loop; you forgot to include $num++ in the original if statement (it's only in the else, so the execution gets stuck at 1).
This is a better way:
<?php
$num = 0;
while ($num < 5)
{
if ($num == 1)
{
echo 'There is' . $num . ' monkey.';
}
else
{
echo 'There are ' . $num . ' monkeys.';
}
$num++;//moved outside the if statement
}
?>

Related

mysql php - check if last variable is the same [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I tried to echo out the $rinse variable, I get nothing
But I do get one for the $rang['email'], any clues would be good
I also tried doing $rinse == $rang['email'];
while($rang = mysql_fetch_assoc($results))
{
if ($rang['email'] = $rinse){
echo $rang['email'];
}
$rinse = $rang['email'];
}
my code updated:
echo $rinse;
if ($rang['email'] == $rinse){
echo $rang['email'];
}
$rinse = $rang['email']
This is still not working for me
You need two = in your if statement.
if ($rang['email'] == $rinse){
You should add double ==. This is comparion, a single = means set to
while($rang = mysql_fetch_assoc($results))
{
if ($rang['email'] == $rinse){
echo $rang['email'];
}
$rinse = $rang['email'];
}

Using PHP variable inside function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I use the following code to get the full filename of a package using only the name of the app:
<?php
// define which app
$app = "appname";
// Search the directory for possible matches
foreach (glob("../../debs/com.dev.".$app."*") as $filename) {
preg_match('/\\d\\.\\d-\\d_.*\\.deb/', $filename, $matches);
$debname = "com.dev.".$app."_{$matches[0]}\n"; }
echo $debname;
?>
The filename of the package ($debname) is then shown correctly in the echo as: com.dev.appname_1.0-1_iphoneos-arm.deb
So i'm then trying to use this $debname variable to get the size of the file:
<?php function format_size($size) {
$sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
if ($size == 0) { return('n/a'); } else {
return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}
echo format_size(filesize("../../debs/".$debname));
?>
For some weird reason the code is not showing the size and it shows n/a
If i set the debname variable manually to $debname = "com.dev.appname_1.0-1_iphoneos-arm.deb" it works fine and i get the file size..
I just can't figure out what's the problem
I tried using SESSION..
session_start();
$SESSION['debname'] = $debname;
...
...
echo format_size(filesize("../../debs/".$SESSION['debname']));
But same issue..
This line is wrong:
$debname = "com.dev.".$app."_{$matches[0]}\n";
^^ new-line character
Note that you are adding a new-line to the end of your string. You should remove that and confirm the correct name with a var_dump($debname); if necessary to make sure there are no white-spaces in between or at the end:
$debname = "com.dev.".$app."_{$matches[0]}";

strpos() not working as expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm checking some html for a string and the result I'm getting is not as expected
$html = "<body>Link</body>";
if(strpos($html, "href=\"http://domain.com") === FALSE)
echo "Not Found";
else
echo "Found";
It always return "Found" even though it's not actually found (I don't want it found) in this example?
Should I be using a different function?
string http://domain32a.com isn't same as http://domain.com. You also have some syntax errors, try:
$html = "<body>Link</body>";
if(strpos($html, "href=\"http://domain32a.com") === FALSE)
echo "Not Found";
else
echo "Found";
Change Your code to this
<?php
$html = "<body><a href='http://domain32a.com'>Link</a></body>";
if(strpos($html, "href=\"http://domain.com") === FALSE){
echo "Not Found";
} else {
echo "Found";
}
?>

I`m sure is a quick fix, but it does not seem to work like this [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem is that the $prenume echoing works, while $comentarii doesn`t. I tried with and without quotes. If I echo $comentarii in the while loop, it echoes. I only have 1 line of text to retreive from database. Please help! Thank you !
$gaseste_elevul = "SELECT prenume_elev, comentarii
FROM elevi
WHERE kod_utilizator=1";
$gaseste_elevul_query = mysql_query($gaseste_elevul);
while($elevul = mysql_fetch_array($gaseste_elevul_query))
{
$prenume = $elevul['prenume_elev'];
$comentarii = $elevul['comentarii'];
}
if($comentarii = NULL)
{
echo "Momentan nu aveti informari pentru $prenume!";
}
else
{
echo "Mai jos aveti informarile pentru $prenume:";
echo "$comentarii";
}//sfarsit else
I also tried to do the whole if($comentarii=NULL) in the while loop, to no result.
Change
if($comentarii = NULL)
to
if($comentarii == NULL)
Your first statement sets the $comentarii to null, and that's why it doesn't echoing anything.

How i can show message between variables? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have variables such as :
$type1=10;
$type2=40;
$type3=70;
.
.
.
and more
but this variables not stable because i get them from forms.
And also I have one variable :
$total=30;
I want to
if `$total` less than `$type1` =>show special message.
if `$total` between `$type1` & `$type2` =>show special message2.
if `$total` between `$type2` & `$type3` =>show special message3.
and more...
$type1=10;
$type2=40;
$type3=70;
$total=30;
$n=10;//count of variables you have
for ($i=0; $i <= $n; $i++) {
$v = $GLOBALS['type'.$i];
$nv = (isset($GLOBALS['type'.$i+1])?isset($GLOBALS['type'.$i+1]):false);
if($total < $v){
echo '$total is lesser than $type'.$i;
}elseif(isset($nv) && $v < $total && $total < $nv){
echo '$total is between $type'.$i.'and $type'.$i+1;
}
}
What's wrong with the usual if-statements? If you're getting the values from a form, you can simply assign those values to different variables and use that in your if statements to make comparisons.
$type1= $_POST['type3'];
$type2= $_POST['type3'];
$type3= $_POST['type3'];
if($total < $type1) {
//show special message
}
if($total < $type2 && $total1 > $type1) {
//show special message 2
}
if($total < $type3 && $total > $type2) {
//show special message 3
}
if(...) {
...
}

Categories