I am new to php and trying to do this:
<html>
<body>
<?php
echo 'Welcome'.$_GET['name'];
?>
</body>
</html>
This is my php code which displays my name entered by using a html form. When submit button is pressed I get this output :- WelcomeNick Watterson
But I am trying to add space between Welcome and Nick. How can I do this in this code?
use the below code
<?php
echo 'Welcome '.$_GET['name'];
?>
is the html entity code for creating an single space character.
You can also use the below code:
<?php
echo 'Welcome '.$_GET['name'];
?>
Will also work because one empty space is interpreted by the browsers directly.
<?php
$strings = array('Hello', 'World!');
// Usual concatenation
echo $strings[0] . ' ' . $strings[1];
// Some implode
echo implode(' ', $strings);
Related
I have a variable given to me by my cms
I want to add one space before that but have tried many things lol and all dont work, below is what I have tried so far.
<?php echo str_repeat('', 1) htmlencode('$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode(' $postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('. $postcode_coveringsRecord['postcode'].) ?>
<?php echo ' ''htmlencode('$postcode_coveringsRecord['postcode'])' ?>
<?php echo htmlencode(' ''$postcode_coveringsRecord['postcode']) ?>
How can I minipulate
where as the variable gives me one blank space prior to the variable content.
cheers for any input
emma
These ones should work
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
or
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".
I need to do something like this:
header("Content-Type: text/plain");
echo <<<EOT
<?php echo 'arbitrary code using ' . $variables . ' and such.';
echo 'finished';
?>
EOT;
The problem is, PHP still interprets the inline PHP as code and tries to execute it. I would like just to see the code printed in the window.
Use Nowdoc, notice the quotes around 'EOT':
echo <<<'EOT'
<?php echo 'arbitrary code using ' . $variables . ' and such.';
echo 'finished';
?>
EOT;
Or use a single quoted string, obviously escaping single quotes in the string:
echo '
<?php echo \'arbitrary code using \' . $variables . \' and such.\';
echo \'finished\';
?>';
You could use highlight_string function, it receives a string containing your php code and outputs html with the syntax highlight colors.
ex:
<?php highlight_string("<?php echo 'hi'; ?>");?>
You have also the function highlight_file, same thing, but receives a string with the file location.
Doc:
http://php.net/manual/en/function.highlight-string.php
http://php.net/manual/en/function.highlight-file.php
How can I use the HTML <code> element to output a block of PHP code, without the page running that PHP code? Eg;
<pre><code>
<?php
// Some super duper PHP code
?>
</code></pre>
I'm creating an API docs page, which features snippets of PHP that anyone wishing to use the API can use as examples, but anything wrapped in <?php> tags runs as an actual PHP function
Use <?php and ?>.
The HTML entities will show up as PHP opening and closing tags when the page is rendered, but PHP will obviously not see them. But you have to html-escape your code anyways, otherwise contained HTML-tags will be rendered. So there should be
<?php echo 'Hello, World.<br>'; ?>
Another way would be to have a string specified by a nowdoc and then output html-escaped (demo):
<?php
$code = <<<'EOC'
<?php
echo 'Hello, World.<br>';
// ...your code here...
?>
EOC;
echo htmlentities($code);
?>
Have look for different approaches at How do I display PHP code in HTML?.
Do this via PHP like so:
<?php
$code = '<?php
echo "Hello, World!";
?>';
echo '<code>' . htmlspecialchars($code) . '</code>';
?>
try something like this:
<?php echo '<?php'; ?>
This may help you.........
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
I had to convert the less-than and greater-than to their HTML name.
<pre><code><?php echo
"<!--
This is the church title to be used in the heading of the web-pages.
Author: John Fischer III of Written For Christ in 2018
Updated:
-->
<?php echo 'Our Little Church:'; ?>" ?>
</code></pre>
I want to write a file to a file and the file contains some PHP code. I don't want the file to run the PHP when someone reads the file. Basically, I want all the text between the <?php and the ?>, plus those tags. Is there any way to do this in PHP? Possibly with strpos? I tried to use strpos; but I couldn't figure it out.
Here's an example:
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>
The easiest way is probably to parse the file using token_get_all, loop through the result and discard everything that's not of type T_INLINE_HTML.
If your <?php ?> tags are always gonna me at the top of your input file, you could just explode the input and write to your output everything around your tags:
Input:
<?php echo "This is the PHP I want removed!"; ?>
<html>
<p>This is what I want written to a file!</p>
</html>
Code:
$inputTxt = file_get_contents($path . $file , NULL, NULL);
$begin = explode("<?php", $inputTxt);
$end = explode('?>', $inputTxt);
fwrite($output, $begin[0] . $end[1] . "\n\n");
?>
Output:
Before
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>
After
<html>
<p>This is what I want written to a file!</p>
</html>
But, if you plan on having more than one set of <?php ?> tags, then you would need to use preg_match:
Input:
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is <?php echo $something; ?> I want written to a file!</p>
</html>
Code:
<?php
$file="input.txt";
$path='C:\\input\\';
$output = fopen($path . "output.txt",'w');
$inputTxt = file_get_contents($path . $file , NULL, NULL);
$pattern = '/<\?php.+\?>/isU';
$replace = '';
$newInput = preg_replace($pattern, $replace, $inputTxt);
fwrite($output, $newInput);
?>
Output:
Before
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is <?php echo $something; ?> I want written to a file!</p>
</html>
After
<html>
<p>This is I want written to a file!</p>
</html>
If you can choose the filename you're writing to, you can write to a .phps file, which won't be evaluated as PHP. If a visitor views the .phps page, they'll be served up a plaintext file that includes everything inside the <?php ?> tags, as well as the HTML.