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>
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
Potion
  • 440
  • 5
  • 18
  • 1
    `function getDateExpected($desired){ $date = date ("d"); if($date << $desired){ echo $desired - $date; }else{ echo $date-$desired; } }` so where are all those called/defined? – Funk Forty Niner Nov 20 '16 at 19:14
  • 1
    `

    getDateExpected(21) days left

    ` no php tags around the function name, if you intend on executing that in its place.
    – Funk Forty Niner Nov 20 '16 at 19:15
  • You need to wrap php code in php tags - `

    days left

    `
    – Sean Nov 20 '16 at 19:15
  • 2
    `if($date << $desired)` - those `< – Funk Forty Niner Nov 20 '16 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. – Funk Forty Niner Nov 20 '16 at 19:18
  • Oops. I come from a C background so I get the operators confused from language to language. Anyway I am running this locally at the moment using MAMP and in a separate php file everything works as intended. However when I did wrap the PHP tags and change the operator from << to – Potion Nov 20 '16 at 19:48
  • Oh nevermind just saw the last comment to change html to .php extension. Thanks! – Potion Nov 20 '16 at 20:13
  • Revisting this question to see if there was activity; the answer (you accepted) below doesn't fully answer the real issues here, to which I have fully addressed. Plus, there wasn't much explanation given for it, making it of very low quality; this for future visitors to the question / answer. I knew that the question went deeper than posted. Call it "instinct ;-) – Funk Forty Niner Nov 20 '16 at 20:41
  • 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 – Potion Nov 21 '16 at 17:54
  • @AdrianDevera You're welcome. I have posted my answer below. – Funk Forty Niner Nov 21 '16 at 20:02

3 Answers3

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)
  • 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. – Potion Nov 20 '16 at 19:49
  • 1
    *"And change this... to this"* - There is a reason, a specific reason actually. – Funk Forty Niner Nov 20 '16 at 20:43
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
Community
  • 1
  • 1
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
  • 1
    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! – Potion Nov 22 '16 at 19:29
  • @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* – Funk Forty Niner Nov 22 '16 at 19:32
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.

itwebdeveloper
  • 579
  • 1
  • 6
  • 13