Recent

Author Topic: [SOLVED] Regarding PHP  (Read 2491 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1761
[SOLVED] Regarding PHP
« on: June 29, 2023, 04:59:59 am »
May I think that PHP is similar to the ASP (Active Server Page) or javascript? With "similar", I mean ASP or javascript is written in a section within HTML, blocked with <%.. %> or <script>... </script>.  I found that PHP is within <?php ... ?>.

Just interested in this, in order to compare PHP and FreePascal in webserver.
« Last Edit: June 29, 2023, 10:21:49 am by egsuh »

abunakov

  • Newbie
  • Posts: 3
Re: Regarding PHP
« Reply #1 on: June 29, 2023, 07:05:38 am »
PHP is similar to ASP in a way. Although I'd rather say that ASP is similar to PHP since PHP was first.
Both use similar constructs to denote code snippets inside an HTML page.

There is a big difference between PHP/ASP and JavaScript though.

Scripts within <script> tags are transmitted from the server to the browser and executed by the browser runtime (i.e. on the client).

In contrast, PHP scripts are executed on the server by PHP interpreter. PHP replaces those <?php ...> snippets with HTML markup. So the the browser will never see any of <?php ...> tags.

ASP work in a similar way.

There is also Node.js. It's a JavaScript runtime which like PHP also runs on the server.

But still it's important to remember which parts of code run where. PHP/ASP/Node.js run on the server. JavaScript enclosed in <script> tags runs in the browser.

Read more about PHP here:
https://www.php.net/manual/en/intro-whatis.php

egsuh

  • Hero Member
  • *****
  • Posts: 1761
Re: Regarding PHP
« Reply #2 on: June 29, 2023, 07:49:21 am »
@abunakov

Thank you for your reply. I know that PHP and ASP are server-side scripts and javascript is client-side script.
Just wanted to check that PHP codes are immersed into HTML pages (like ASP, node.js, or javascript codes). With this much information, I can imagine how it works (whether right or wrong :D) enough to compare with Free Pascal's web working. Thanks again.

Thaddy

  • Hero Member
  • *****
  • Posts: 18797
  • Glad to be alive.
Re: Regarding PHP
« Reply #3 on: June 29, 2023, 10:00:37 am »
I can imagine how it works (whether right or wrong :D) enough to compare with Free Pascal's web working. Thanks again.
That means you still do not fully understand it:
1. FreePascal has bindings for all the serverside scripting either PHP, JavaScript, Java (sec) or others.
2. FreePascal can also be natively used serverside, which can be often faster.
3. FreePascal has even Pas2JS, which enables JavaScript programming using Object Pascal code.
4. FreePascal even supports compiled webassembly code through a native cross compiler on multiple platforms.

So whatever you prefer is already provided in standard FreePascal libraries and (cross) compilers. It is just up to you what you find the better option for your task at hand.
I do not know of any compiler that supports all the above options.
« Last Edit: June 29, 2023, 10:04:28 am by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

egsuh

  • Hero Member
  • *****
  • Posts: 1761
Re: Regarding PHP
« Reply #4 on: June 29, 2023, 10:21:32 am »
@Thaddy,

I've written several web server programs with Free Pascal, and nothing else.  I'm checking how to use others like PHP, Node.js, etc. to explain the characteristics of my approach.

Thaddy

  • Hero Member
  • *****
  • Posts: 18797
  • Glad to be alive.
Re: [SOLVED] Regarding PHP
« Reply #5 on: June 29, 2023, 10:30:01 am »
I do about the same what you do, but I often run server-side performance tests and these differ per job. I use  e.g. node.js + pas2js for kiosk (full screen javascript applications) and php4fpc to interface some code with my web servers or (fast)CGI applications on my web servers, etc.
It is really down to analyzing the job at hand, the platform(s), all those things.
Usually a customer is tight to many restrictions and that is my guide input.

At the moment I am more and more experimenting with webassembly, but that is just because I have the time (65+) and can put in the effort: I only have some consulting jobs and most of those are not programming, but security training. (SEH, SCSP)
« Last Edit: June 29, 2023, 10:32:03 am by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Warfley

  • Hero Member
  • *****
  • Posts: 2040
Re: [SOLVED] Regarding PHP
« Reply #6 on: June 29, 2023, 11:01:40 am »
PHP is really great to quickly built a small webpage (and since the last few versions PHP as a language isn't aweful anymore, it now even has a static type system)

Basically PHP files will be sent to the PHP interpreter. Plaintext in the file will be directly sent to the client, but as soon as the interpreter finds a <?php (or <? for short) it will switch to PHP mode and interpret everything that follows as PHP until it reaches the corresponding ?>.

For example if you have a list of articles you want to display:
Code: PHP  [Select][+][-]
  1. <ul class="Articles">
  2. <? foreach ($article in load_articles()) { ?>
  3.   <li>
  4.     <p class="HeadLine><? echo $article->name; ?></p>
  5.     <p class="Summary><? echo $article->summary; ?></p>
  6.     <a href=<? echo "'".$article->url.'"'; ?>>Read More...</a>
  7.   </li>
  8. <? } /* End of foreach */ ?>
  9. </ul>

So if your goal is to quickly setup a webpage with some dynamic parts, you will probably be done in PHP before you've even setup your fp-web project.
That said, when you aren't building a template based backend (e.g. instead of HTML sites you write a REST server), this this easy templating around which PHP is built does not really benefit you.

Powerful templating engines is something that is often missing in many languages

egsuh

  • Hero Member
  • *****
  • Posts: 1761
Re: [SOLVED] Regarding PHP
« Reply #7 on: June 29, 2023, 11:27:15 am »
Quote
I only have some consulting jobs and most of those are not programming, but security training. (SEH, SCSP)

I'm expecting getting some advices or guides from you (or others here) in near future.


Thaddy

  • Hero Member
  • *****
  • Posts: 18797
  • Glad to be alive.
Re: [SOLVED] Regarding PHP
« Reply #8 on: June 29, 2023, 12:35:08 pm »
Unless you missed something, I have been doing that for 25 years:
proper wiki entries
bug reports
accepted patches

and regarding security a LOT of answers on this forum why some things do not work anymore and how to fix it.

I also do stupid things but mostly they were pointed out by PascalDragon.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

egsuh

  • Hero Member
  • *****
  • Posts: 1761
Re: [SOLVED] Regarding PHP
« Reply #9 on: July 01, 2023, 04:43:57 am »
I haven’t done writing web server with other tools than fpc but looking at the structure of ASP or PHP,  I think one advantage of fpc is that I can separate web page design from what is called business logic, e.g. db operations. I can keep a separate datamodule which contains db and related codes which can be shared by other applications.

Warfley

  • Hero Member
  • *****
  • Posts: 2040
Re: [SOLVED] Regarding PHP
« Reply #10 on: July 01, 2023, 11:59:18 am »
I haven’t done writing web server with other tools than fpc but looking at the structure of ASP or PHP,  I think one advantage of fpc is that I can separate web page design from what is called business logic, e.g. db operations. I can keep a separate datamodule which contains db and related codes which can be shared by other applications.
You can also do this with PHP and ASP. Usually you will have your whole buisnesslogic in a seperate PHP directory and files, which are then included by your frontend files that also contain the HTML.

For example from one of my websites, a frontend facing file:
Code: PHP  [Select][+][-]
  1. <?php
  2. // Including internals
  3. set_include_path(__DIR__ . "/../");
  4. require_once "internal/utils.php";
  5. require_once "internal/users.php";
  6. require_once "internal/session.php";
  7. require_once "internal/requests.php";
  8.  // ...
  9. ?>
  10. [...] // Usage in HTML
  11. <table>
  12.             <?php
  13.             $request_id = create_request($conn, $REQUEST_VALIDITY);
  14.             ?>
  15.             <tr><th>User</th><th>Rights</th><th>Password</th><th>Delete</th></tr>
  16.             <?php
  17.                 $users = User::all_users($conn);
  18.                 foreach ($users as $user) {
  19.             ?>
  20.             <tr>
  21.                 <td><?php echo $user->name; ?></td>
  22.                 [...]  // Filling table with user data
  23.             </tr>
  24.             <?php } ?>
  25.         </table>
  26.  

Where the class User in users.php then abstracts away all the database functionality:
Code: PHP  [Select][+][-]
  1. <?php
  2.  
  3. class User {
  4.  
  5.     public readonly int $uid;
  6.     public readonly string $name;
  7.     private string $salt;
  8.     private int $access_level;
  9.  
  10.     function __construct(array $row) {
  11.         $this->uid = $row["ID"];
  12.         $this->name = $row["Name"];
  13.         $this->salt = $row["Salt"];
  14.         $this->access_level = $row["AccessLevel"];
  15.     }
  16.     [...]
  17.     static public function all_users(PDO $conn): array {
  18.         $result = array();
  19.         $query = $conn->query("SELECT ID, Name, Salt, AccessLevel FROM Users");
  20.         foreach ($query as $row) {
  21.             $user = new User($row);
  22.             array_push($result, $user);
  23.         }
  24.         return $result;
  25.     }
  26.  

This way I can use this users file in all of my subpages and have all the database stuff in one place

egsuh

  • Hero Member
  • *****
  • Posts: 1761
Re: [SOLVED] Regarding PHP
« Reply #11 on: July 01, 2023, 12:36:30 pm »
Quote
You can also do this with PHP and ASP. Usually you will have your whole buisnesslogic in a seperate PHP directory and files, which are then included by your frontend files that also contain the HTML.

Thank you for your information but I have no intention to study PHP or ASP at all. Pascal only is too much for me^^.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [SOLVED] Regarding PHP
« Reply #12 on: July 03, 2023, 01:34:24 am »
I haven’t done writing web server with other tools than fpc but looking at the structure of ASP or PHP,  I think one advantage of fpc is that I can separate web page design from what is called business logic, e.g. db operations. I can keep a separate datamodule which contains db and related codes which can be shared by other applications.
As Warfley said, you can also do that in ASP/PHP. The difference is, it's optional there, while it can be said a must in FPC (technically, it's possible to go down the same route, DelphiWebScript already does it, for example). However, experience tells us that ain't a good way and the modern approach to web programming largely avoids it, for that separation of concerns reason. A semi-ideal approach is to use programming language agnostic templates, like Mustache, where we have an implementation for in fcl-mustache (need FPC trunk AFAIR). This can be used both as server and client side template, which is still quite flexible although frontend developers will likely to prefer direct DOM manipulation in JavaScript.

egsuh

  • Hero Member
  • *****
  • Posts: 1761
Re: [SOLVED] Regarding PHP
« Reply #13 on: July 03, 2023, 03:03:55 am »
Currently I'm using javascript heavily. Probably that's why I'd like to separate data and design.

Warfley

  • Hero Member
  • *****
  • Posts: 2040
Re: [SOLVED] Regarding PHP
« Reply #14 on: July 03, 2023, 10:59:00 am »
Seperation of Logic and Design is great, as I have shown, I also do this with a templated website in PHP. The thing is, personally I think that most a website should be fully functional without javascript, i.e. where all of the contents can be generated by the server. I work in IT-Security, and at work we have JavaScript generally disabled for security reasons. And I find it really sad that so many pages that have no reason to rely on Javascript do so. For example many news websites today don't function without JS, even though the whole purpose of their Website is to simply display data without being interactive in any way.

The Problem is, for creating the pages on the server side, you need a good templating engine. I have used Pascal for a few Websites, but it was sorely lacking in that regard. It very often boiled down to generating the HTML strings myself in code (e.g. for a list of articles, I would literally just concatinate entry templates together).

PHP isn't a great language, but through it's template driven design, you will be able to create webpages really fast. A basic website that fetches data from a MySQL database and displays it in a list is done in absolutely no time. Also, while PHP is often the butt end of jokes regarding security, PHP provides alot of typical security functions you need in the web of the cuff, like a cryptographically secure random number generator, hash functions and more generally it has up to date OpenSSL bindings.
So when you just want to get a small website done quickly, PHP is probably the best option out there

 

TinyPortal © 2005-2018