Help me
How can I insert a function inside a single quote?
<php
echo ' Hello world <?php function(); ?> ';
what's the correct syntax ?
If you have to use single quotes then you would concatenate (.) the function:
echo ' Hello world ' . function();
You have to cancatenate it.
echo 'Hello '. yourFunction() . 'world !';
Related
I want to insert a URL all into single quotes. I have tried using both single and double quotes, but it is not working. I checked this question - Escaping single quote in URL link, but it did not help:
<?php
echo '\'' base_url().'\'index.php?admin/product/delete/1\'';
echo '\''; echo base_url().index.php?admin/product/delete/1\'';
echo '\'. base_url().index.php?admin/product/delete/1\'';
echo "\'"; echo base_url().'index.php?admin/product/delete/1\'";
?>
I am trying to achieve this:
'http://localhost/my_site/index.php?admin/product/delete/1'
The URL in single quotes.
If you really want to use only single quotes, try this:
echo '\'' . base_url() . 'index.php?admin/product/delete/1\'';
Just simple:
echo "'" . base_url() . "index.php?admin/product/delete/1'";
You can use double-quotes to pass variables into strings in PHP. On top of enhancing readability, this also saves you the trouble of escaping single quotes.
So for example:
<?php
$foo = 'bar';
echo 'single-quoted string: $foo <br />';
echo "double-quoted string: $foo <br />"; // "$foo" works, "{$foo}" is better
echo "double-quoted string: \$foo <br />"; // Backslash escapes
?>
Prints (emphasis mine):
single-quoted string: $foo
double-quoted string: bar
double-quoted string: $foo
So to update your example:
<?php
/* ... */
$baseurl = base_url();
echo "'{$baseurl}index.php?admin/product/delete/1'";
?>
Or how about we put it all into a variable and gain a little readability:
<?php
/* ... */
$baseurl = base_url();
$fullurl = $baseurl . 'index.php?admin/product/delete/1';
echo "'{$fullurl}'";
?>
echo "'" . base_url() . "index.php?admin/product/delete/1'";
Im echoing some data from a database using PHP. However the data is too close together and needs a space in between each one.
while($book = mysql_fetch_array($books)) {
echo '<div>'
.$book['title']
.$book['author']
.$book['genre']
.$book['price']
.$book['availability']
.'</div>';
}
Is their a way to print a break maybe after each one to give a space.
Cheers
You can print it as html entity :
echo '<div>'
.$book['title'] . ' '
.$book['author'] . ' '
.$book['genre'] . ' '
.$book['price'] . ' '
.$book['availability']
. '</div>';
yes, and the answer is shown below
while($book = mysql_fetch_array($books)) {
echo '<div>'
.$book['title']." "
.$book['author']." "
.$book['genre']." "
.$book['price']." "
.$book['availability']." "
.'</div>';
}
To add my five cents )
while($book = mysql_fetch_array($books)) {
echo "<div>{$book['title']}
{$book['author']}
{$book['genre']}
{$book['price']}
{$book['availability']}</div>";
}
You may use the curly syntax in the double quoted strings:
echo "<div>{$book['title']} {$book['author']} {$book['genre']} {$book['price']} {$book['availability']}</div>";
When a string is specified in double quotes or with heredoc, variables
are parsed within it.
How do I add a space in the returned text between sample1 and sample2? Here is what I have so far:
if ($eventid!="") {
echo $get_event['sample1'], $get_event['sample2'];
}
It's really simple, just echo a space between the variables...
<?php if($eventid!=""){echo $get_event['sample1'] , ' ', $get_event['sample2']; }
Indentation always make things cleaner and easier:
if (!empty($eventid)) {
echo $get_event['sample1'] . ' ' . $get_event['sample2'];
}
On PHP, you need to use a dot (.) to concatenate strings... as you can see on the Strings Operators documentation:
http://php.net/manual/en/language.operators.string.php
How can i echo " i mean if i want to show: "Patrik", i would do:
<?php echo " "Patrik" "; ?>
but as you know you cant do this
Escape double quotes with \ when full string itself is in double quotes:
<?php echo " \"Patrik\" "; ?>
Or:
<?php echo ' "Patrik" '; ?>
More Info:
PHP: Double quotes vs Single quotes
http://pt.php.net/manual/en/language.types.string.php
<?php echo '"Patrik"'; ?> or.. <?php echo "\"Patrik\""; ?>
<?
echo " \"Patrik\" ";
// or
echo '"Patrick"';
?>
if it's HTML or XML, you can use " as substitute for the double-quote.
I was wondering how can I print out whitespace in the following code below.
<?php echo "$first_name", "$last_name" ; ?>
Just, well, add whitespace to your output?
<?php echo "$first_name $last_name with some whitespace" ; ?>
Sometimes, it the obvious answer is right - even in PHP!
If you want to concatenate several strings without the double quotes (e.g. when using single quotes):
$string = $first_name . ' ' . $last_name; // The dot is a concatenation operator
<?php
echo "$first_name $last_name";
?>
Notice that when you use ' any var aren't display, with " does.
So, good is:
<?php echo "$first_name $last_name" ; ?>
bad
<?php echo '$first_name $last_name' ; ?>
If you want to keep using commas:
<?php echo $first_name, ' ', $last_name; ?>