I have a simple if checking for a customer_id in the session.
<?php if($this->Session->read('customer_id')){ ?>
<?php echo $this->element('watchlist'); ?>
<? } else { ?>
<?php echo $this->element('recently_sold'); ?>
<?php } ?>
It's very simple, but when I visit the page, the customer_id is null, and neither of the elements show up. When I login, BOTH the elements are displayed. Something is really strange with the logic here and I'm not sure where to start. I tried checked for null and using is_numeric, but still the same results. Has anyone had any issues with sessions on their local environment like this?
Thanks
Does your PHP set-up recognise the PHP short tags <? ... ?> as opposed to <?php ... ?> ?
If it doesn't then your } else { statement will not be recognized as PHP and so both echo's will happen when the if-statement is true (and you should see the "<? } else { ?>" printed out as text in your source code if you look carefully).
Like #Chris Hendy says above it's confusing to keep opening and closing PHP tags for no reason.
Short tags should not be used as they are a server setting and most servers do not have them on by default. From PHP 5.4 short tags are on a switch with the difference that the "echo short tag "
Related
I trying to put user data from PHP in HTML form, like name, email, address, etc. But i don't know what i have to do to work it.
Already tried:
<?php
session_start();
$test='some name';
$_SESSION['name']='some name';
?>
<!-- AND -->
and nothing happened.
SOLVED CODE:
<?php
session_start();
$test='some name';
?>
<a><?= $test ?></a>
The PHP manual has a page introducing the PHP tags:
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them.
Note that "interpreting the code" is not the same as displaying the result of that code. For instance, you could write <?php $test = 'hello'; ?> to assign a new value to a variable, and it won't cause anything to be output.
To output a string, you can use the echo and print keywords. Note that these are not functions, so do not have parentheses around their arguments; as noted in the manual, including redundant parentheses will sometimes work, but can be misleading. So the following output the variable $test into the page:
<?php echo $test; ?>
<?php print $test; ?>
As the PHP tags page I linked to earlier says:
PHP includes a short echo tag <?= which is a short-hand to the more verbose <?php echo.
So you can also use:
<?= $test ?>
However, you also have a second problem, which is nothing to do with PHP, and will happen if you hard-code a URL into your HTML like this:
Nothing will appear in the browser! Why? Because you've defined the destination of a link, but haven't defined any text or content to click on; you need it to look like this:
Click me if you dare!
So put that together, you might write this in PHP:
<?= $linkDescription ?>
Change this
to this
or to this if you want to see the data on the html page
<?php $test; ?>
<?php echo($test); ?>
Do note that there are better ways to display such data. Check this out:
https://www.w3schools.com/php/php_echo_print.asp
i am a bit confused when i use conditional statements like
<?php
if(isset($_POST['somevalue'])){
?>
<h1> this is out side php mode now </h1>
<?php
}else {
?>
<h1> again its out php mode </h1>
<?php
}
?>
but yet it still works i mean if $_POST['somevalue'] is set then it outputs "this is out side php mode now" if not it outputs "again its out php mode" my question is if i am outside php mode how does it work then?
I think your question is "how PHP works".as we know php is a server side language.it executes in the server but the scope of the html code will be inside the if loop.so
<?php
if(isset($_POST['somevalue'])){
?>
will be evaluated in server and not in the html part and that will be either true or false.
so after the execution in server your code in front end i.e. in the html part will be like this
<?php
if(1){
?>
<h1> this is out side php mode now </h1>
//as above code is markup language so it will be interpreted by the browser
<?php
}else {
?>
<h1> again its out php mode </h1>
<?php
}
?>
NOTE: the delimiters is for the server to know that the code inside the tag is php code and it will execute it accordingly.
Although you have closed off the executable section of the PHP code, the surrounding if statement and the curly braces will actually have a higher priority as to what is executed and what isn't.
<?php
if
{
// This is considered inside the statement
// and will only be sent if the execution
// makes it inside the statement.
?>
...
<?php
}
else
{
}
?>
// Anything here is simply sent to the browser
// as it will always executed.
<?php
// more code etc
?>
Anything inside the IF statement is considered part of the IF - even if it contains close/open PHP tags.
Basically the PHP control structure overrides open/close tags. This means any sort of if, switch, function etc etc has a higher priority than open close tags.
That's one thing I love about php.
Initially, the main reason is as #Fluffeh mentioned that you are still within the "if" statement.
One way I could put it is that, PHP allows to be embeded in side HTML code. As long as the file has a .php extention then (someone correct me if I'm wrong) Apache knows to use the PHP processor to process that file. It will process the php coding and display the HTML sections in it as well.
Your question is some what similar to
<?php
$name = "Tom";
?>
<h1>Hello <?php echo $name;?>!</h1>
The result will come out as Hello Tom!
it is the same idea as it is inside the php.. satisfy each of the condition and it will run its corresponding statement..
this is one of the best features of php, which allow us to use native html codes rather than putting it inside echo.
A PHP file is processed as plain text/html until it reaches a <?php tag when it executes the php code. When it reaches the closing ?> it processes it as plain text again.
This is exactly the same, but with echo and quote marks around the html you want to print. If you're having trouble reading the code I suggest indenting as needed.
<?php
if(isset($_POST['somevalue'])){
echo "<h1> this is out side php mode now </h1>";
}else {
echo "<h1> again its out php mode </h1>";
}
?>
The way you posted it.
<?php
if(isset($_POST['somevalue'])){
?><h1> this is out side php mode now </h1><?php
}else{
?><h1> again its out php mode </h1><?php
}
?>
This is the most random thing I have ever seen, but when I print a variable in page using a simple method like:
<?= $appCnt ?>
<? echo $appCnt ?>
It gets rendered as:
<!--?= $appCnt ?-->
<!--? echo $appCnt ?-->
I have reinstalled PHP and the crazy thing is everywhere else on the page it is working fine. If I echo this before page load it renders fine, but I even have other stuff in page that is rendering properly.
If I do the following, it works but why on earth is that?
<?php echo $appCnt ?>
Thanks for any help.
I believe you need ot enable short open tags
http://php.net/manual/en/ini.core.php#ini.short-open-tag
Have you checked to see that short_open_tag are enabled in PHP? Or, are you using PHP >= 5.4?
I'm fairly new to PHP. I started learning it like 3 weeks ago. I cannot find the answer to this question on StackOverflow, Google or Youtube. The PHP documentation to this just confuses me. To get on with the question, how does PHP code mixed in with HTML work?
<?php if (something) { ?>
<p>Hello</p>
<?php } ?>
The p element will only display if something has a truthy value, how is this?... I thought for sure that the PHP engine ignored what was going on around the outside of the codeblocks (e.g. <?php ?>) and only parsed what happens on the inside.
The code below gets parsed by the PHP engine normally and sent to the browser without affecting any HTML elements (even though its clearly between 2 code blocks).
<?php echo $something; ?>
<p>Hello</p>
<?php echo $something; ?>
I hope I'm not going to get flamed for asking this question since a lot of people seem to understand how it works in like a tenth of second.
P.S. I asked this question in chat early and thought I understood it correctly but when I went to implement it my mind was still like, how does this work exactly? It just seems like some kind of hack to me.
Easy now. Definitely need a php tutorial for you to start on http://www.tizag.com/phpT/
Here is what your doing:
<?php
//Anything inside me php processes
if($something)
{
echo "<p>something</p>";
}
//About to stop processing in php
?>
<p>Anything outside of the php statement above will just be printed to the dom</p>
Quick Note: It is good practice to separate your PHP from your HTML
<?php if ($something) { ?> <-- where is the other {
<p>Hello</p>
<?php } ?> <-- oh I see it.
In your first example it is indeed true that <p>Hello</p> will be rendered if and only if 'something' returns true.
If you close a php tag with ?> but have an 'unclosed' execution, like if (blah) { ..., the PHP engine understands your desires and does accordingly.
Why?
The PHP engine is kept 'waiting' until the execution is closed with } and then the final result is evaluated and the browser continues on with the lines below.
Obviously if you leave out the final } you will see some errors, which tells you that PHP was expecting you to finish what you started and you did not
Both the php and html are parsed in-line. So, as it moves down your script it will run php scripts within tags, and display html in the order which they are placed. For example:
<? $someVar = "someVar string value"; ?>
<h1>This is a title</h1>
<? if(1 == 1){?>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<? } ?>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: <?=$someVar;?></p> // <?= is a short hand for echo
This will show as:
<h1>This is a title</h1>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: someVar string value</p>
Basically just think of it as the server reading down your script and parsing whatever it sees as it goes. If there is html, it will display it and if there is php which does some sort of calculation and then spits out html, it will show the spat out html.
You can write php code anywhere in HTML using php code block
<?php echo "whatever " ?>
or
<?php echo "<h1>Here everything will displayed in h1 </h1> "; ?>
and if you use control structure ( if, switch etc.. ) then it will behave like all other languages, means if something is true then it will execute the part written between { }.
so if you write an undefined variable in if condition then it will not execute code block of because undefined variable is treated as false condition .
additionaly you can check any variable value by var_dump($variable)
PHP's Alternative syntax for control structures
<!DOCTYPE html>
...
<div>
<?php if ( the_thing === true ) : ?>
<p>The thing is true! \o/</p>
<?php else if ( the_other_thing === true ) : ?>
<p>The other thing is true! meh</p>
<?php else : ?>
<p>Nothing is true :-(</p>
<?php endif; ?>
</div>
...
I am working with levels of security with my app and i have written a function that simply checks - depending on it's session user id what kind of priviligies he/she has. It works fine but in some pages i want to output some information if the user is superuser, and forbid to output information if user is a guest.
I do it with such a syntax:
1. <? if admin('superuser', $_SESSION['user_id']) { ?>
2. <div></div>
3. <? } ?>
It works good but it's not elegant, and in case long code between curling brackets it messess with purity of my code.
Is there a way to "alias" a line 1 and 3 to some kind of shortcut, ie
1. <? admin_superuser ?>
2. <div></div>
3. <? admin_super_user_end ?>
Maybe you have some other ideas to perform such levels of security?
The idea came from ob_start() and ob_end() commands.
I am waiting for your ideas.
Kalreg.
you could simply set a bool at the beginning of the page:
$isSuperUser = admin('superuser', $_SESSION['user_id']);
Then, just do
<? if ($isSuperUser) { ?>
<div></div>
<? } ?>
If you don't like the $, you could define a constant:
define("SUPERUSER", admin('superuser', $_SESSION['user_id']));
Then, just do
<? if (SUPERUSER) { ?>
<div></div>
<? } ?>
Good thing about a constant is that it is global, and if using in a function, you wouldn't have to declare it global first, or pass it as an argument.
I would go with something like this. I think this totally acceptable.
To simplify it you just need a wrapper for your user.
<?php if ($user->isAdmin()): ?>
<div></div>
<?php endif; ?>
you can include another php file that contains the corresponding html / php code with the "include" function. i also recommend to use <?php instead of just <? due short open tag issues with xml and ini settings.