php print statements in separate lines - php

The following php script prints several statements and variables! they print on the sameline! i want to print them in separate lines one after the other! How can i do it?
for example the 1st 2 statements should be printed as:
Programming in PHP is fun
I learnt how to comment in PHP
but currently it prints as
Programming in PHP is fun I learnt how to comment in PHP
?php
echo "Programming in PHP is fun";
//This is the 1st programming 4 tutorial
/*This tutorial helped me
to gain a good knowledge in php! */
print "I learnt how to comment in PHP";
echo "A variable name must start with a letter or an underscore '_' -- not a number";
echo "A variable name can only contain alpha-numeric characters, underscores (a-z, A-Z, 0-9, and _ )";
echo "A variable name should not contain spaces. If a variable name is more than one word, it should beseparated with an underscore or with capitalization ";
$name= "My name is XXX";
$number=25;
$float_number=10.245;
$negative_number=-12;
echo($name);
echo($number);
echo($float_number);
echo($negative_number);
$age=24;
$feature =$age;
echo($feature);
$x=45;
$y=12;
echo($x+$y);
$myName="My name is 'Lasith'";
?>

If you echo them as plain text, add a newline character after each echo:
echo "\n";
If you echo them to HTML (into a web browser), add a HTML <br> tag after each echo:
echo "<br>";
You can also use string concatenation
echo "Programming in PHP is fun" . "\n";
echo "Programming in PHP is fun" . "<br>";
or add the newline or <br> tag to the string
echo "Programming in PHP is fun\n";
echo "Programming in PHP is fun<br>";

Related

using fpdf : str_pad doesn't work with spaces, it works with other characters

I am adding a pad to my string, to fill with spaces, but it doesn't work
the code is here
<?php
$string1 = "Product 1 ";
$newString = str_pad($string1,100);
echo $newString."test";
echo "<br>";
$string2 = "Product 2222 ";
echo str_pad($string2,100," ")."test";
echo "<br>";
?>
the output is like this:
Product 1 test
Product 2222 test
You could try $str = str_pad($string2,(100*strlen(" "))," ")."test"; instead.
renders to a non-breaking-space in html (and when writing to document with fpdf).
Please note this can only work with fpdf when you tell it to write all lines as html! And the encoding should be utf-8 probably
$fpdf->Write(iconv('UTF-8', 'windows-1252', html_entity_decode($str)));
When the output of the PHP is converted to HTML, all the white spaces except the first are removed and it is the default feature of HTML and web browsers. so the output will not be correct.
You have to use the " " instead of white space in the str_pad function. HTML don't ignore the " " and against each existance of it, HTML adds a white space to the string.

Newline is not coming while echoing [duplicate]

This question already has answers here:
Why does the browser renders a newline as space?
(6 answers)
Closed 8 years ago.
I have started learning PHP. In my first code the newline is not coming properly. I have gone through the PHP doc, but still I'm not getting the problem.
<?php
# Echoing
echo "Hello World to PHP \n";
echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php";
# Variable Basics
$name = "Vivek Kumar";
$age = 26;
echo "My name is $name and age is $age .";
echo 'My name is '. $name . ' and age is ' . $age . '.';
?>
output:
Hello World to PHP Concatenation in PHP is done using .Vivek
kumarLearning phpMy name is Vivek Kumar and age is 26 .My name is
Vivek Kumar and age is 26.
I assume you echo the output into a html page you look at with a web browser? In that case the linebreaks do get copied to the output, however they are not visualized as such. This simply is because in html markup a linebreak is something different to a plain text file.
Check about the use of <br> or <br /> linebreaks for such markup. Also php offers the handy nl2br() function for such purpose.
An example for such output (see it here):
Hello World to PHP Concatenation in PHP is done using .
<br />
Vivek kumarLearning phpMy name is Vivek Kumar and age is 26 .
<br />
My name is Vivek Kumar and age is 26.
But in general you should think about whether you really simply want to concatenate such strings when putting them in to html markup. Typically you want to wrap them in (invisible) containers like spans, divs or paragraphs, so that you can control the final layout by using styles (style sheets / css).
An arbitrary example (see it here)
HTML:
<div id="intro">Hello World to PHP Concatenation in PHP is done using .</div>
<h2>Vivek kumarLearning php</h2>
<div class="plain">My name is Vivek Kumar and age is 26 .</div>
<div class="plain">My name is Vivek Kumar and age is 26.</div>
CSS:
body {
font-size: 130%;
}
#intro {
font-weight: bold;
margin-bottom: 10px;
}
.plain {
font-size: 100%;
}
You can use nl2br to convert new line (\n) to line break (<br>).
From http://php.net/manual/en/function.nl2br.php:
string nl2br ( string $string [, bool $is_xhtml = true ] )
Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).
Or you can just use line break, as the others have suggested. nl2br is handy when showing text from a form text area.
if you are using this in a browser you should use <br> tag or you should use <pre>
I am posting the answer with <pre> and with <br>
**with `<pre>`**
<?php
# Echoing
echo '<pre>';
echo "Hello World to PHP "."\n";
echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php";
echo '<pre>';
with
echo "Hello World to PHP "."<br/>";
echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php";

PHP replace hyphen with space [duplicate]

This question already has answers here:
how to replace hyphen with blank space / white space? php
(2 answers)
Closed 8 years ago.
So, I have a piece of code I am using in most of my pages which sets the page title as the file name. This is because I don't want to have to change the title each time I create a new page.
This all works fine, except I don't want to have spaces (or rather, %20) being shown in the url when the file name contains spaces. I'd much rather have hyphens in place of the spaces, as it looks cleaner to the user. However, this means PHP will set the page title also with hyphens instead of spaces, which, frankly, looks ugly.
Is there a way I can rewrite this code to replace the hyphens with spaces?
The code:
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst($path_parts['filename']);
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use str_replace to replace the - with a space.
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst(str_replace('-', ' ', $path_parts['filename']));
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use implode adn explode for this
$string = "the string";
$arr = explode("string to replace",$string);
$string = implode("string to add ",$arr);
echo $string;

echo php variable and html in Wordpress

I am playing around with wordpress and wondering, why this line of code works:
echo "<a href='....'>$name</a>";
I learned it this way:
echo "<a href='....'>".$name."</a>";
Is there something special defined in WP to make this work?
In PHP, there are two types of String.
The first type uses the single quotes, as follows.
$val = 'this is a simple string';
The second type is as follows:
$val = "This is a not so simple string";
With the latter type, any variables included in the string will be resolved to their values, so:
$val = "Hello there";
$message = 'Dave says $val';
// Literally equals: Dave says $val
$message2 = "Dave says $val";
// Literally equals: Dave says Hello there
There are lots of other differences, which you can read about here.

how to combine function and string inside php echo

Can you combine a PHP function and string in the same echo statement? Currently, I have this code that grabs a photo's caption, then shortens it to 25 characters or less, stopping at the nearest blank space so it can fit the whole word.
echo substr($caption,0,strpos($caption,' ',25));
echo " ...";
Example output: changes "This is way too long to fit in this foobar small area preview box" to "This is way too long to..."
I'd like to be able to combine the 'substr' and '...' into the same echo statement. I tried the following, but it didn't work:
echo "{substr($caption,0,strpos($caption,' ',25))} ...";
Any ideas?
The , is great for this, and much faster than the ., which has the overhead of concatenating the string.
echo substr($caption, 0, strpos($caption, ' ', 25)), '...';
EDIT: To clarify, the , just simply sends all the strings, separated by the comma, to echo, and thus is equal to the separate line echo statments. The DOT operator performs concatenation. You can use as many commas as you want, i.e. echo strFunction1(), ' some text ', strFunction2(), '...';
Try:
echo substr($caption,0,strpos($caption,' ',25)) . '...';

Categories