Why does php echo print out ?> message? - php

With the following code below,
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
I want only the Hello World statement to be printed, but it also prints out ;?>.
After I inspected the element, it came out like this.
<body>
<!--?php echo '<p-->
Hello World
<p></p>
"; ?>"
</body>

Your website is treated as plain HTML. Ensure that you have a webserver running:
If you have accesst to the command line on a Gnu/Linux derivate you can start PHP's internal server by:
php -S 127.0.0.1:1337
This command has to be started in your web root and you should have an index.php living there. Then your code runs fine.
In the long run, you want to set up a nginx or an apache2. Another aspect to look into is using vagrant to setup a virtual Linux machine that creates your development enviroment.

You are trying to open the file directly in your web browser, but unlike HTML, your browser is incapable of understanding PHP.
PHP needs to be run server-side behind Apache, Nginx or any viable web server, your web browser will then request pages from that webserver, which will request PHP to serve your browser the fully generated page.
Practically, if you're trying PHP for the first time, you want to install a development LAMP stack such as WampServer (for Windows only), XAMPP (for Linux, Windows, OS X) or MAMP (for OS X, Windows). Installing and running this will easily set up a web server on your computer.
I will not write a full tutorial on how to use a LAMP stack but I can recommend you to read that article from Kasia Mikoluk on Udemy. It should teach you the basics.

Related

Apache server running html code but not recognizing php code [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 3 years ago.
I just started to learn php. I am using lubuntu and that's why I don't want to install xampp, wamp, or similar programs because I have very limited resources.
I installed Apache server and php 7, which seems to be working fine because I can execute and visualize html code via localhost. The problem is when I run php code the web page shows blank. How can I fix this issue?
I have only one file on /var/www/html which is called index.php and can be visualize on localhost/index.php
php -v
php 7.2.19-0ubuntu0.18.10.1
apache2 -v
Server version: Apache/2.4.34 (Ubuntu)
The code I have on index.php is this:
<!DOCTYPE html>
<html>
<body>
something
<?php
echo "hello world";
?>
</body>
</html>
On localhost/index.php it only shows "something", which means apache server is running correctly and runs html code but I don't know why php doesn't run. If Instead I just run a file with php code like
<? php
phpinfo();
?>
it only shows a blank page.
If you see a blank page then normally something else is blowing up behind the scene. I suggest you check both Apache and PHP error logs. Normally you would find both in /var/log/*
It could very well be permission related or a module missing.

How to run a win32 console application on Linux/Apache web-hosting?

The executable uses 2 un-managed dlls & gives output on console.
I'm currently running it on XAMPP/localhost (on my PC) & grabbing the console output with PHP's Exec()/PassThru() function.
Will it run the same way on a typical Apache/Linux based Web-hosting account? If not , what can I do?
In PHP :
<?php
exec("TradeLogin.exe",$output);
//TradeLogin.exe is the win32-console-app,
//situated at Xampp/htdocs, with couple of supporting binary files & dlls
//(compiled on Visual-Studio-2015)
echo $output[0]."<br/>";
echo $output[1]."<br/>";
?>
I can't tell if there will be permission problems with it (I'm on a Windows atm. so can't try.)
If you want it to run this way, you need a Windows emulator (like Wine for example), and use it like
exec("wine TradeLogin.exe 2>&1",$output);
Or full path may be necessary. Note that the execution of the PHP code will stop until your program has finished running.

PHP not executing on web host

My web host supports PHP. I have successfully run info.php and get pages of stuff to prove it in my browser.
When I try to execute a tiny piece of PHP in a tiny piece of HTML, as in the following hallowed script, I see nothing at all in my browser. Why? Is there something I am supposed to do?
Note: I use RapidWeaver as my principal site generation tool.
<html>
<head>
<title> PHP Test Script </title>
</head>
<body>
<?php
print "Hello World!";
?>
</body>
</html>
Make sure that your file extension is indeed .php, so that the web-server knows how to handle the file. Also, check your error-logs, there might be something in there if you get no output at all. You can do that in your terminal by running php --info | grep error. On your file-system, the error-log is stored in either /var/log/httpd/error_log or /var/log/apache2/error_log (if you're on Linux).

Can't connect to localhost using Chrome with Uniform Server

I'm trying to run a .php web application on Google Chrome, using Uniform Server. I thought that just having that installed would make PHP run, but clearly there's a lot I don't know. First off, when I opened the file from file:///C:/Users/... etc the PHP didn't execute properly. Here's the code:
<html>
<body>
Hello to you, good sir.
<?php
echo "</br> Hello there yourself.";
?>
</body>
</html>
Here's the output on the website:
Hello to you, good sir. Hello there yourself."; ?>
And here's how the <body> code looks from Chrome:
Hello to you, good sir.
<!--?php
echo "</br-->
Hello there yourself.";
Then, when I tried accessing the file from localhost, Chrome wouldn't even connect, saying:
Oops! Google Chrome could not connect to localhost
I'm aware of this question, but the OP didn't appear to have software needed to run PHP. I'm pretty sure I have the right software, but I don't know how to use it. How do I get Uniform Server to run PHP, and how do I connect to localhost from Chrome?
PHP wont work if opened directly in browser, thats not how PHP works, it needs to be run through the PHP parser.
A Step by step:
Download the Latest package
Unpack it onto desktop or C:, (somewhere your find it)
Open up the UniServerZ folder, where you unpacked it
Double click UniController.exe
Click Start Apache button - it should go green like below
Remove the junk files in UniServerZ/www
Put your PHP files inside that www folder.
Visit http://localhost to view your PHP files

PHP or WAMP not sure what

I have installed WAMP server 2.0 with (PHP 5.4.3). After installing WAMP I have restarted all the services and can open
phpinfo() - it is showing up fine
phpmyadmin - it is also showing up fine.. i can play with database..
however when run simple php file in chrome I could see the code rather than result. below is the code i am trying..
<?php
echo "hello world";
?>
below is the link to file which I am trying to access via chrome.
file:///C:/WAMP/www/hello%20world.php
Make sure you have started apache
Try http://localhost/hello_horld.php
To run this script, start a browser use URL http://localhost/your_file.php or http://127.0.0.1/your_file.php
PHP is a server-side technology (instead of client-side technology like JavaScript). The PHP statements <?php ... ?> are processed in the server, and the results returned to the client (browser). so you need to use it through server not direct

Categories