I do not understand this PHP Code, what is it doing? [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
How to read php
i'm learning php, I wrote this. (It is from a book)
<?php if(!empty($data)): ?>
<ul>
<?php foreach ($data as $dataprint): ?>
<li><?= $dataprint ?></li>
<?php endforeach ?>
</ul>
I am unable to understand some of the code in this section.
1. Why are there colons on line 1 and 3?
2. What does the '<?= $dataprint ?> ) and why does it not have the standard 'PHP' word?
3. why is there an equal mark in the next?

colons - this is a shorthand version of statements, you should not really learn them at such an early stage. First you need to learn the full versions.
<?= $variable ?> is a short version of <?php echo $variable ?> but means exactly the same.
Also, the shordhand versions from question one are actually not considered a good practice, because when you nest them they are hard to read.

Colons on lines 1 and 3 are an alternative way of doing the below code
if (!empty($data)) {
...
}
It makes it a lot cleaner when outputting HTML.
(See http://php.net/manual/en/control-structures.alternative-syntax.php)
<?= is a shorthand way of writing <?php echo, again it's just an alternative way of doing things.
(See http://php.net/manual/en/function.echo.php)

You can write loops in php many type...
1)eg: if(condition){...code...}else{...code...} or if(condition): means, in place of "{" you can use ":" and for ending the loop use "endif"
2) Your point no 2 is written in shortcode in php for that you have to enabled shortcode in php.ini

Related

What is difference in <? and <?php [duplicate]

This question already has answers here:
Difference between <?php and <?
(7 answers)
Closed 6 years ago.
I've got some old legacy code written in PHP 5.3.
All PHP blocks in this code are like
<? some_php_code_here ?>
and my Apache just ignores them, while works good with
<?php some_php_code_here ?>
Why old code contains invalid blocks, and why this old code works good on old server ? It's really hard to google something with so much special symbols, sorry for stupid question
<? is called a php short tag
Here is what the manual has to say about it
PHP also allows for short open tag <? (which is discouraged since it
is only available if enabled using the short_open_tag php.ini
configuration file directive, or if PHP was configured with the
--enable-short-tags option).
You can enable them at runtime with <?php ini_set('short_open_tag',1) ?>
But if you do so you won't be able to use inline xml like so <?xml ?>
and instead you can use
<?php echo '<?xml version="1.0"?>'; ?>
As a side note I do often use:
<?= "Hello world" ?>
<?= 'foo' ?> is equivalent to <?php echo 'foo' ?> and looks much cleaner in template files in my opinion
Since php version 5.4, <?= is no longer tied to short_open_tag but prior to that it was, so care should be taken if you might be running your code on older php versions

What is the meaning of this symbol in php [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
PHP echo vs PHP short echo tags
(6 answers)
Closed 9 years ago.
What is the use of the following symbols in php?
<?= code($data); ?>
Though I don't know the actual use of the above symbol, I found out that I can use
<?= my_function($data1,$data2); ?>
instead of using
<?php echo my_function($data1,$data2); ?>
Can anyone explain what exactly is happening here and what is the use of it?
It's a quick way of echoing text, as that's one of the most common uses of PHP.
As of PHP 5.4, <?= ?> is essentially the short-hand way of writing <?php ?>.

What is <?=$varname?> syntax in PHP code? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
Previous I bumped into codes like this:
<?php
$var="hello";
?>
<?=$var?>
It simply prints out the content of $var, so... is the syntax equivalent to echo $var?
I'll also appreciate an answer pointing to a related manual page. Since the syntax is not searchable.
Yes, <?=$var?> is the same as <?php echo $var; ?>
From PHP.net manual: echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
You can read more here.

<?php, <?, <?=, whats the difference? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I don't know if this has been asked before, but here goes: What's the difference between <?php, <?=, and <??
I'm just curious, and people have been telling me to use <?php instead of <?, but I just use <? because it works for me and it's faster. I ain't seein' no diff'rence (redneck accent for extra pleasures...).
<?php and <? are equivalent, except it's generally preferred to use <?php since not every server configuration may have the short opening PHP tags option enabled.
<?= is a shortcut for echoing back the value of a variable/expression, i. e.
<?=$var ?>
is similar to
<?php echo $var; ?>

Better to write <?php or <? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are PHP short tags acceptable to use?
So I noticed that you can write PHP programs either like
<?php
// do something
?>
or
<?
// do something
?>
What's the difference and what's the best practice?
I always use
<?php
because in some hosts the short tags are not allowed. This avoids some headaches.
There's no difference at all. Except
<?= $var ?>
that acts like an "echo"
You should use <?php whenever possible, with short_open_tag=off (see php.ini) <? just won't work and code will be treated as html.

Categories