Echo in one line in PHP - php

I want to change the original code I have,
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (";
echo metersToFeetInches($productarea) . " ft²)" . "</p>";
into a single echo line as shown here:
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (" . metersToFeetInches($productarea) . " ft²)" . "</p>";
But I'm getting some strange line breaks in this second case for metersToFeetInches($productarea).
Generated HTML:
24,757
<p>
<strong>Area:</strong>
2,300 m² ( ft²)
</p>
Output:
24,757
Area:
2,300 m² ( ft²)
How can I solve it? Is there any documentation I could read to learn how to do it by myself in the future?

I'm pretty sure I know what's going on here, your function metersToFeetInches is echoing a value rather than returning it.
function metersToFeetInches() {
echo 'OUTPUT';
}
echo 'FIRST '.metersToFeetInches().' LAST';
// Outputs: OUTPUTFIRST LAST
echo metersToFeetInches() is actually redundant.
This is because the function runs before the string you built is actually output. Note that both examples you posted would have this problem. Change your function to return a value instead. Afterwards, any places where you have used it like so:
echo 'Something';
metersToFeetInches();
echo 'Something Else';
You'll have to use an echo:
echo 'Something';
echo metersToFeetInches();
echo 'Something Else';
Functions should pretty much always return a value. Lesson learned, perhaps?
If you are really in a bind and cannot change the function, you'll have to resort to output buffering:
ob_start();
metersToFeetInches($productarea);
$metersToFeetInches = ob_get_clean();
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (" . $metersToFeetInches . " ft²)" . "</p>";
...which is rather silly to have to do.

Related

How to set a third operator in a String that adds the Text "Uhr"?

I need to add "Uhr" at the end of the following Code:
<?
$doc = JFactory::getDocument();
$doc->setTitle($event->title . " | Example");
$doc->setDescription($event->title . " am " . $event->start, 'end' -> $event->end "uhr needs to be here");
?>
how can I archive this? Because if I add a . "Uhr" it doesn't work :(
$doc->setDescription($event->title . " am " . $event->start . ", " . $event->end . " Uhr");
should do it.
Formatted dates:
$doc->setDescription($event->title . " am " . date('d.m.Y', strtotime($event->start)) . ", " . date('H:i', strtotime($event->end)) . " Uhr");
I think there is something wrong with the last ->.
Maybe this works:
$doc->setDescription($event->title . " am " . $event->start, 'end' . $event->end . "uhr");
In case "am", "end" and "urh needs to be here", best you use the same quotes like:
<?
$doc = JFactory::getDocument();
$doc->setTitle($event->title . " | Tanzschule Hartung");
$doc->setDescription($event->title . " | Tanzschule Hartung" " am " . $event->start, "end" -> $event->end . "uhr needs to be here");
?>
(look at your text 'end', compared to the code above)
It looks like $event->start (and others) need to display some text which is requested.
Why you need to use a . (dot) after requesting text from another function is because PHP needs to understand when it has to display text or not.
The . is the string concatenation operator.
This might work as well:
<?
$doc = JFactory::getDocument();
// Setting titles
$title = $doc->setTitle($event->title);
$start = $event->start;
$end = $event->end;
$doc->setDescription($title . " | Tanzschule Hartung am " . $start . " , end" . $end . "uhr needs to be here");
?>
I hope this clears something up.
If you have any questions, feel free to ask.

PHP -displaying 12 items of an associative array with specific key and value

<?php
foreach($catalog as $id=> $item) {
echo get_item_html($id,$item);
}
// here is the functions page code :
function get_item_html($id,$item){
$output = "<li><a href='#'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
. "<p>View Details</p>"
. "</a></li>";
return $output;
}
?>
I have used it for displaying product categories i checked several time but I did find any error but it display undefined variable
please help me to find out this solution

date_create_from_format AND DateTime::createFromFormat functions breaking on Server

I recently uploaded my client's site onto a temporary server so that they could get started on data input while I fine tune the design.
When I launched it however, one of my sliders and one of my pages broke down completely [note: this only occurs on the server side; my localhost site continues to work perfectly].
I narrowed it down to my use of the date_create_from_format() function as I use it on both pages, and when I remove the element holding that bit of php, the site works fine. I have scoured my file for any missing semi-colons, or brackets, and I can't find any glaring errors. Here is my code as it was orginally on my localhost.
<p>
<?php
$end = date_create_from_format('Ymd',$ending_date);
$start = date_create_from_format('Ymd',$starting_date);
echo "<span class='month'>" . $start->format('F') . "</span>";
echo " ";
echo "<span class='day'>" . $start->format('j') . "</span>";
echo ", ";
echo "<span class='year'>" . $start->format('Y') . "</span>";
echo " - ";
echo "<span class='month'>" . $end->format('F') . "</span>";
echo " ";
echo "<span class='day'>" . $end->format('d') . "</span>";
echo ", ";
echo "<span class='year'>" . $end->format('Y') . "</span>";
echo ", $location";
?>
</p>
I have also tried converting it from the object to the procedural function (See below) but the result is the exact same.
<p>
<?php
$end = DateTime::createFromFormat('Ymd',$ending_date);
$start = DateTime::createFromFormat('Ymd',$starting_date);
echo "<span class='month'>" . date_format($start,'F') . "</span>";
echo " ";
echo "<span class='day'>" . date_format($start,'j') . "</span>";
echo ", ";
echo "<span class='year'>" . date_format($start,'Y') . "</span>";
echo " - ";
echo "<span class='month'>" . date_format($end,'F') . "</span>";
echo " ";
echo "<span class='day'>" . date_format($end,'d') . "</span>";
echo ", ";
echo "<span class='year'>" . date_format($end,'Y') . "</span>";
echo ", $location";
?>
</p>
Thank you "Sysix" for the answer regarding assigning a timezone. I made the following adjustment (See below) but the problem persists.
<?php
$end = DateTime::createFromFormat('Ymd', $ending_date, new DateTimeZone('America/Toronto'));
$start = DateTime::createFromFormat('Ymd', $starting_date, new DateTimeZone('America/Toronto'));
?>
Anyone have any ideas?
From your comment: Your PHP Version ist 5.2.17
but DateTime::createFromFormat is avaible since PHP 5.3
Look at the doc: http://php.net/manual/en/datetime.createfromformat.php
I have found a notice, that DateTime::createFromFormat ignore the setted TimeZone. You must set the Timezone explicit for the object.
http://php.net/manual/de/datetime.createfromformat.php#116027

PHP concatenation issues

So I am really bad at concatenation Ive been working on it for an hour the best i can get this to do is no errors butit doesnt echo the username for some reason I am pretty sure it has to do with concatenation anyway.
Code:
echo '<span style="color: #"' . $row['color'] . ';"';
echo $row['user'] . ': ' . '</span>' . $row['message'] . '';
echo '</br>';
Basically I am trying to make the username show the color of the the hex in database. But when I do this it doesnt even show the username just the message.
echo '<span style="color: #' . $row['color'] . ';">'
. $row['user'] . ': </span>' . $row['message']
. '</br>';
You didn't close the <span> tag.
You can also just output the entire string and interpolate the values:
echo "<span style=\"color:#{$row['color']}\">{$row['user']}:</span> {$row['message']}<br>";

PHP: $_POST["startId$i"]) != ""

I am trying to process a form that is dynamically created and therefore varies in length. The while loop seems to work fine. However, the 'if' statement is not; it should only print the startId$i and corId$i if and only if the form's particular text field was filled in. The code is printing a line for every text field on the form, regardless of if it was left empty or not.
$i = 0;
while(!is_null($_POST["startId$i"])){
if(($_POST["startId$i"]) != ""){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}
$i = 0;
while(isset($_POST["startId$i"])){
if( !empty($_POST["startId$i"]) ){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}
Can you manage with fields names ?
If yes, better way is to name inputs with name="startId[0]" and name="corId[0]" and so on...
Then in PHP you just do:
$startIds = $_POST['startId'];
$corIds = $_POST['corId'];
foreach ( $startIds as $k => $startId ) {
if ( !empty($startId) ) {
$corId = $corIds[$k];
echo "startId: " . $startId . " ---<br>";
echo "corId: " . $corId . " ---<br>";
}
}
You should use empty() in this case:
if(!empty($_POST["startId$i"])) {
...
}
I suggest to check the real content of $_POST. You can do that via var_dump($_POST);
You may find out, for example, that the empty fields contain whitespaces. In that case the trim() function may help.
For example:
while(isset($_POST["startId$i"])){
if(trim($_POST["startId$i"])){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}

Categories