Question mark equals, doesn't work on php - php

this is my php code:
<html><body>Hey!: <?= "World";?></body></html>
It just prints "Hey!:" Whats wrong with my code?

Short tags (which you're using here) can be turned on or off depending on the server you're running the code on. If it's your server, look in php.ini

You need to set short_open_tag to 1
http://php.net/manual/en/ini.core.php

Does your file end in .php and will it execute as php on your webserver? add
<?php echo "yes I run php!<br>\n"; ?>
to your file to be sure. View source to see what happened to the php tags. Then maybe switch on short tags as the other answers told you to.

Related

Newbe PHP: I'm haveing trouble running simple example code

I'm trying to get some PHP example code to work on PHP version 5.3.4, Apache 2.2.17 on Windows.
The example says I need PHP 4.0 and above with CURL and contains:
<?
$function = $_GET['function-if-exist'];
$test = "Test";
?>
<? =$test ?>
I don't understand why I'm getting the following errors:
My PHP doesn't understand <? and wants <?PHP instead.
My PHP doesn't like <? =$test ?> and wants something like
<?PHP echo $test ?>
$function = $_GET['function-if-exist']; causes the error "Undefined index" but presumably works for the folks that developed it.
Can anyone help me understand why their code is not working for me?
1) <? is the "short tag". Most servers are configured to not allow short tags. This can be changed in php.ini.
2) Again, short tags. Also I think you can't have a space before the =, but the main problem is the short tags setting.
3) $_GET accesses the query string, so when loading your script you need myscript.php?function-if-exist=something
It is more ideal to check if the parameter is set before continuing to prevent errors being thrown, e.g.
if(isset($_GET['function-if-exist']))
{
$functionexists = $_GET['function-if-exist'];
}
the short tag notation is disabled in your php.ini
you need to remove the space before your equal sign
your _get array contains not the expected index, what url do you enter to access the page?
I don't understand why I'm getting the following errors:
My PHP doesn't understand
To be able to use short tags you will have to enable them via config ... http://www.tomjepson.co.uk/tutorials/35/enabling-short-tags-in-php.html
My PHP doesn't like and wants something like
Once you switch on the short tags you will be able to echo using ... important the equals signs must be touching the ? not variable.
$function = $_GET['function-if-exist']; causes the error "Undefined index" but presumably works for the folks that developed it.
The $_GET is populated according to what is in the url. To get a value in $_GET['function-if-exist'] the url accessing the script should be something like mydemo.php?function-if-exist=hello
Hope this helps you
Quick answers to 1 and 2 are enable the short_open_tag option into the php.ini file, for the last one is set the error_reporting to a less strict mode.
The reasons of not to adopt such measures are:
the short tag clashes with the xml declaration and is disabled on different host, if you need to manipulate xml or if you need to write portable code is better to resort to the long tag syntax. You lose the ability to echoing data with = but it is a small annoyance to me.
Warning and notices, as php forgive a lot the programmer for missing variables declaration are a blessing for debug. Keep then raised and you will address a lot of mispellings.
Are you sure that function-if-exist is a correct index for your hash? I would check the index first the access them. If the index don't exists is a probable hint that something is going wrong with your code and you should check the reason of the missing.
Better to stop now, as anyone can write a book on this topic, and several ones already done ;)

php.ini: what i must change, to write <? instead <?php?

in my local hosting the script doesn't work, if i wrote <? instead of <?php.
what i must change in php.ini to correct it?
Thanks
http://php.net/manual/en/ini.core.php
short_open_tag
Is the property you seek.
It is:
short_open_tag
Make sure to read:
PHP Short Open Tag: Convenient Shortcut or Short Changing Security?
It's already been said how to do it, but I'm just saying that it might not be such a great idea. Almost anyone you ask will tell you not to use short tags. Unless you have a specific reason to use short tags, you should just type <?php. It's only 3 more characters.

php short_open_tag problem

hi i am working on a project of my college but my college server admin told me that we don’t use short_open_tag in our code what’s its mean? what is short open tag and what is alternate of it
thanks
Normally you write PHP like so: <?php PHP CODE HERE ?>. However if allow_short_tags directive is enabled you're able to use: <? PHP CODE HERE ?>. Also sort tags provides extra syntax: <?= $var ?> which is equal to <?php echo $var ?>.
Short tags might seem cool but they're not. They causes only more problems. Oh... and IIRC they'll be removed from PHP6.
You can try the tool at:
http://blog.eezgu.com/php-short-open-tag-sorunu-ve-cozumu.html
This is a PHP command line tool that converts short tags to normal PHP tags.
The page is not English but you can click the link in the post and download the tool.

PHP. "?>" on top of page

I am creating site with php.
On localhost all works well.
On my hosting all looks good too, but on top of page i see "?>". In my code these symbols are absent.
What is this?
It could be that your code uses short open tags (<? instead of <?php) and your hosting provider has short open tags turned off. That would mean, however, that your PHP code is not interpreted at all. It could also mean that your hosting provider doesn't support PHP at all, or only for certain file types.
Take a look into the page's source code to check whether that is the case.
The fact that you see that on top of the page could mean one or more things.
It seems you have typed in ?>
outside of a php block
You may be using short tags <?
instead of long <?php and the host
has short tags turned off
Out of these its most likely you have a closing ?> in your code without a corresponding open <?php tag
Are you sure yu're seeing ?> and not something like >>? ?
Otherwise, this smells like a PHP-EndTag which was never opened...check your code.
If you have a empty lines in your sourcefile before the opening <?php-tag, then those empty lines could get outputted unintenionally. If your script should start with <?php on top, remove all the empty lines above it.

PHP is displaying my source code?

I just upgraded to php5.3 and when I execute:
php myfile.php
I see the source code for myfile.php.
Any idea why this is happening? I've never seen anything like it before!
In 5.3
short_open_tag
is disabled by default. So if you use
<? ?>
instead of
<?php ?>
it can cause issues in the future (provided you just re enable the flag in php.ini). From what I have read they are still debating removal from 6.
Are you sure you didn't forget the opening PHP tag at the begining of the file ?
ie :
<?php
echo "hello, world!";
(Note I used "full" open tags ; ie "<?php", and not short open tags "<?" -- maybe you used short open tags, and those are disabled ? If I remember correctly, they are disabled by default, in PHP 5.3)
and not :
echo "hello, world!";
(If that doesn't help : can you post a short code sample that reproduces the problem ?)

Categories