String Length Issue [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have four string length statements I am trying to validate,but only two are working.
$name = "mike";
$age = "54";
$city = "Chicago";
$building = "Madadnock";
$lengthOfString1 = strlen ( $name );
echo "String length of 'name' " . $lengthOfString1 . "</br>";
$lengthOfString2 = strlen ( $age );
echo "String length of 'age' " . $lengthOfString2 . "</br>";
$lengthOfString3 = strlen ( $city );
echo "String length of 'city' " . $lengthOfstring3 . "</br>";
$lengthOfString4 = strlen ( $building );
echo "String length of 'building' " . $lengthOfstring4 . "</br>";
The top two are the only ones that work. Any help would be great!

variables in PHP are case sensitive
$lengthOfString4 vs $lengthOfstring4
^ ^

The problem lies in variable names. The variable names in PHP are case-sensitive.
$lengthOfString3 = strlen ( $city );
echo "String length of 'city' " . $lengthOfString3 . "</br>";
$lengthOfString4 = strlen ( $building );
echo "String length of 'building' " . $lengthOfString4 . "</br>";
Fixed it.

Related

Can't Sort PHP Array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anybody tell me why I can't get this to work?
I am simply trying to sort my arrays from highest to lowest.
$stuff[] = "100";
$stuff[] = "104";
$stuff[] = "102";
$stuff[] = "103";
$stuff[] = "101";
$stuff[] = "99";
echo "Largest: " . max($stuff) . " <br> \n";
arsort($stuff);
echo "0 : " . $stuff[0] . " <br> \n";
echo "1 : " . $stuff[1] . " <br> \n";
echo "2 : " . $stuff[2] . " <br> \n";
echo "3 : " . $stuff[3] . " <br> \n";
echo "4 : " . $stuff[4] . " <br> \n";
echo "5 : " . $stuff[5] . " <br> \n";
arsort() is sorting your array, but it also conserve the keys of each value. Witch mean the key 0 will still answer with 100, and not the first element !
With a foreach, you would see them in correct order.
foreach ($stuff as $value) {
echo $value . "<br>\n";
}
OR
You can also use rsort() that will not be associative, which mean the keys won't have the same values after it.

Resolve PHP post string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following post string:
Name=John&Age=33&Question1=What's+your+gender%3F&Answer1=Male&Question2=What's
+your+education%3F&Answer2=Graduate
I'm going to have randomly generated amount of questions each time so I won't be able to know number of passed variables. Thus, I am searching for a solution something like:
foreach($postItem as $varr) {
echo $varr["name"].": ".$varr["value"]."<br />";
}
Post them as answer[1], answer[2] etc instead of answer1, answer2, and PHP will turn them into an array for you.
//parse query string to an array ($output)
parse_str($postString, $output);
echo $output['Name'] . ': ' . $output['Age'] . '<br />';
//process questions
$i = 1;
while (array_key_exists('Question' . $i, $output)) {
echo $output['Question' . $i] . ': ' . $output['Answer' . $i] . '<br />';
$i++;
}
function used http://php.net/parse_str

Error with extra { in PHP elseif statement [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
Having trouble locating the problem in this elseif
Code checker suggests an extra } however I cannot find it
if (get_field('job_salary') == "Competitive") {
echo "Competitive";
} elseif (get_field('job_salary') == "P.A") {
echo the_field('job_salary_singular');
} elseif (get_field('job_salary') == "P.A Range"){
echo "£" . the_field('job_salary_range_start') . " - " . thefield('job_salary_range_end');
} elseif (get_field('job_salary') == "Per Day"){
echo "£" . thefield('job_salary_day_rate') == " per day";
} elseif (get_field('job_salary') == "Per Day Range"){
echo "£" . the_field('job_salary_day_range_start') . " - " . the_field('job_salary_day_range_end') . " pd"
}
There is no semi-colon on your last 'echo' line. :) Therefore, the last } appears to be extra.

Please specify how to implement this code between echo ' '; [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have using this code between echo "";
<?php
for ($i=0; $i<$tot; $i++)
{
$rt = date ( "d-m-Y", $y[$i] ) ;
}
?>
Please specify how to implement this code between echo "";
<?php
$output = 'for ($i=0; $i<$tot; $i++){ ';
$output .= '$rt = date ( \'d-m-Y\', $y[$i] );';
$output .= '}';
echo $output;
?>
For readability I separated it into 4 lines, but you can see how it could easily be refactored into one echo statement.
The key idea here is that by declaring strings with ' instead of ", php will not interpret the $ signs as variables. Additionally, in order to include single quotes within the string itself, a backslash escape character is required as in \'d-m-Y\'

Get part of string in PHP without explode [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the following string
$s = "hellomyname";
$result = ...
How can I get the "my" out of this the fastest way possible?
You have to find the position of ((my)) in your string with strpos() function and then fetch the word with substr().
here is an example:
<?php
$s1 = 'laldkfjamydnadjacv zdvzkv';
$pos = strpos($s1, 'my');
echo 'First Position---> ' . $pos;
$my1 = substr($s1, $pos, 2);
echo '<br /> this is result---> ' . $my1;
//second test
$s2 = 'hellomyname';
$pos2 = strpos($s2, 'my');
echo '<br />Second position---> ' . $pos2;
$my2 = substr($s2, $pos2, 2);
echo '<br />this is second result---> ' . $my2;
?>
and this is result:
First Position---> 8
this is result---> my
Second position---> 5
this is second result---> my
I think you're looking for substr($s, 5, 2).
Explode (Delimiter, text)
In your case can use:
$result = explode('my', 'hellomyname'); // array([0] => 'hello', [1] => 'name');
If you need get the last value you can put end():
$result = end(explode('my', 'hellomyname')); //name
Reference
http://www.php.net/manual/pt_BR/function.explode.php
http://us1.php.net/end

Categories