2

Im new to Php. Im sure this is a simple issue. I am trying to call the function getDateExpected within the tags of my html file. Id like for it to return the number as a text like below. How is this done? I see that the php script would be run using some event like a button clicked but in this case nothing is being clicked. The page is only loaded. Im aware of javascript using { } but is this possible for php or is it only meant to handle requests from a form?

<?php
    //include "landing.php";
    function getDateExpected($desired){
      $date = date ("d");
      if($date << $desired){
        echo $desired - $date;
      }else{
        echo $date-$desired;
      }
    }

?>

<!DOCTYPE html>
<html>
<title>Coming Soon</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
    background-image: url('obg.png');
    min-height: 100%;
    background-position: center;
    background-size: cover;
}
</style>
<body>

<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
  <div class="w3-display-topleft w3-padding-large w3-xlarge">
    Potion
  </div>
  <div class="w3-display-middle">
    <h1 class="w3-jumbo w3-animate-top">COMING SOON</h1>
    <hr class="w3-border-grey" style="margin:auto;width:40%">
    <p class="w3-large w3-center">getDateExpected(21) days left</p>
  </div>
  <div class="w3-display-bottomleft w3-padding-large">
    <a href="http://www.twitter.com/" target="_blank">Adrian</a>
  </div>
</div>

</body>
</html>
11
  • 1
    function getDateExpected($desired){ $date = date ("d"); if($date << $desired){ echo $desired - $date; }else{ echo $date-$desired; } } so where are all those called/defined? Commented Nov 20, 2016 at 19:14
  • 1
    <p class="w3-large w3-center">getDateExpected(21) days left</p> no php tags around the function name, if you intend on executing that in its place. Commented Nov 20, 2016 at 19:15
  • You need to wrap php code in php tags - <p class="w3-large w3-center"><?php getDateExpected(21); ?> days left</p> Commented Nov 20, 2016 at 19:15
  • 2
    if($date << $desired) - those << don't do what you expect them to do. Commented Nov 20, 2016 at 19:17
  • also make sure you are running this under a php environment with a webserver installed and using the proper extension to parse php directives. Commented Nov 20, 2016 at 19:18

3 Answers 3

2

"Thank you for the assistance Fred! You have fully answered and addressed all the issues but unfortunately I was not able to select your answer as it was only a comment ): If you would make a proper answer I will select yours as correct for future visitors that would want a better understanding – AdrianDevera"

(This is a late answer to fully outline the code's failure, and as requested by the OP to post it):

There are a few issues with the code you posted.

Let's go over these one by one:

if($date << $desired)

The << characters (in PHP, seeing in comments that you are from a C background) is a bitwise operator and will not do what you expect them to, as in "check if the date is less than x"
( < is less than):

if($date < $desired)

Then we have this:

<p class="w3-large w3-center">getDateExpected(21) days left</p>

The getDateExpected() function for it isn't wrapped in php tags <?php ?>, therefore it is never called/executed, it is simply a string right now; looking at your HTML source, you would have seen exactly that, along with unparsed PHP code.

Therefore, change it to the following:

<p class="w3-large w3-center"><?php getDateExpected(21) ;?> days left</p>

In addition to this, and as seen in comments; you apparently were trying to run this as an .html file extension and this is something that did come to mind when seeing the question, as per a comment I left:

"also make sure you are running this under a php environment with a webserver installed and using the proper extension to parse php directives."

That file extension by default will not parse PHP directives, .php will and inside a PHP environment along with a webserver installed.

Yet, it is not impossible to run .html files as PHP if you instruct your server to treat them as PHP and if it is supported.

To run this inside a php environment and on a live (local) server, the syntax is:

  • http://localhost/file.php as opposed to file:///file.xxx
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Fred for the detailed and thorough explanations in your multi-part solution. Really simple mistakes I made that anyone else should definitely learn from. Accepted yours as the full and complete answer!
@AdrianDevera You're most welcome and it's always nice when someone such as yourself, appreciates a well-detailed answer in order to learn from it and not being thrown code "just cuz" and to "just fix and go", thanks for that, cheers
2

As Fred-ii- commented you need PHP tags change this

<p class="w3-large w3-center">getDateExpected(21) days left</p>

to this

<p class="w3-large w3-center"><?php getDateExpected(21);?> days left</p>

And change this

  if($date << $desired)

to this

if($date < $desired)

2 Comments

Thanks. I changed the following and the value doesnt display within the desired location after making changes. Is there anything else I am doing wrong? If I call the function within a php file, it works as intended but not within the HTML file itself.
"And change this... to this" - There is a reason, a specific reason actually.
1

Your web server tries to execute the .html as a static page because this extension is not associated with the PHP language and thus is not parsed with the PHP engine. To do so you will need to make sure the page has a .php extension (associated with the PHP engine).
As you want to execute the .html page instead you may want to activate the mod rewrite on your web server.
If you are using Apache you can do this following the instructions here.
You then want to instruct Apache to redirect all the *.html requests to the corresponding *.php file without showing the browser the real extension (.php). To do so you can follow the instructions in here. In this way the user will digit in his browser the url http://www.example.com/foo.html and the web server, once received the GET request for the file foo.html will match the corresponding .php script (same name, different extension) and automatically execute the page foo.php with PHP without showing the user browser a change in the URL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.