Using PHP variable inside function [closed] - php

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]}";

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'];
}

php if empty string do something [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 having trouble making a empty string to have something inside, I'm also removing some other unnecessary characters from that string which works.
$desc = strip_tags($mapAnnotationArray);
$mapAnnotationArrayOutput = str_replace( array('"', '(' , ')'), '', $desc);
$mapAnnotationArrayOutput = trim($mapAnnotationArrayOutput);
if(empty($mapAnnotationArrayOutput)) {
($mapAnnotationArrayOutput == "empty");
}
Change this
($mapAnnotationArrayOutput == "empty");
To this
$mapAnnotationArrayOutput = "empty";
I've seen many people write:
if( x = "foo")
and wonder why it assigns "foo" to x... but never the other way around.
if( !$mapAnnotationArrayOutput) $mapAnnotationArrayOutput = "empty";
Try putting:
print("String is empty");
In the if statement, at the moment ($mapAnnotationArrayOutput == "empty"); will have no output so you wouldn't know if the string was empty or not.
If you're trying to assign the variable the value of "empty", use:
$mapAnnotationArrayOutput = "empty";
Instead.

Loop not counting [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 am trying to get the following code to add how many people can retire and not. the code displays but does not add. What am I doing wrong?
<?php
$canRetire= 0;
$notRetired = 0;
$agesFile = fopen("ages.txt", "r");
$nextAge = fgets($agesFile);
while (feof($agesFile) ){
list($agesFile)=explode(":",$nextAge);
if ($agesFile > 65){
$canRetired = $canRetired + 1;
}
else{
$notretired = $notRetired + 1;
}
$nextAge = fgets($agesFile);
}
fclose ($agesFile);
print("<p>Number of people can retired : $canRetired</p>");
print("<p>Number of people not retired: $notRetired</p>");
?>
Shouldn't it be while(!feof(...))?
You want to stop when you reach the end of file.
Plus it looks like you've got some spelling errors:
$notretired = $notRetired + 1;
Notice how the R is lower case on the left and upper case on the right.
Also at the beginning you have $canRetire and in the if condition inside the loop you have $canRetired.
And just another little hint for you: $notRetired = $notRetired + 1; is the same as $notRetired++;
Made a couple modifications to cases, and what appears to be a type based on the variables you have defined. Looks like the variable $canRetire does not match $canretired.
Using while(feof($fh)) is basically saying, do this for the EOF. Which renders that entire loop useless. Using while(!feof($fh)) will allow you to loop until the EOF.
<?php
$canRetired= 0;
$notRetired = 0;
$agesFile = fopen("ages.txt", "r");
$nextAge = fgets($agesFile);
while (!feof($agesFile) ){
list($agesFile)=explode(":",$nextAge);
if ($agesFile > 65){
$canRetired = $canRetired++;
}
else{
$notRetired = $notRetired++;
}
$nextAge = fgets($agesFile);
}
fclose ($agesFile);
print("<p>Number of people can retired : $canRetired</p>");
print("<p>Number of people not retired: $notRetired</p>");
?>

PHP: Basic loop [closed]

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
}
?>

Syntax error - unexpected ":" [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
My php script directs to a url depending on which submit button was pressed. However, when I run the test I'm getting an error saying line 4 contains an unexpected ":" but my line 4 is my header script with the url?
I'm confused because I have other scripts similar to this and they don't give me that error. Can anyone tell me what I'm missing, might be simple, I have been caught being simple before.
<?php
if ($_REQUEST['Dish1'] == 'Dish1')
{
header(“Location: http://blahblah”.urlencode($_POST[‘uid’]));
}
else if ($_REQUEST['Dish1'] == 'Dish2')
{
header(“Location: http://blahblah2”.urlencode($_POST[‘uid’]));
}
else if ($_REQUEST['Dish1'] == 'Dish3')
{
header(“Location: http://blahblah3”.urlencode($_POST[‘uid’]));
}
etc.....
?>
You are using curly quotes.
Replace all the “ ” and ‘ ’ to " and ' respectively.
You are using the wrong quotes... use "" instead of “”. Refer to Wikipedia, you must use typewriter quotes, not curly or inverted commas.
PD: Also PHP Parse error: syntax error, unexpected '.' on line 15 ; )
Replace you code with following
<?php
if ($_REQUEST['Dish1'] == 'Dish1')
{
header("Location: http://blahblah.urlencode".($_POST['uid']));
}
else if ($_REQUEST['Dish1'] == 'Dish2')
{
header("Location: http://blahblah2".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish1'] == 'Dish3')
{
header("Location: http://blahblah3".urlencode($_POST['uid']));
}
?>
Is it not much easier to write:
$lookup = array('Dish1' = > 'http://blba1', 'Dish2' = > 'http://blba2');
if( isset($lookup[$_REQUEST['Dish1']]))
header("Location: " . $lookup[$_REQUEST['Dish1']]);

Categories