CAP33
Posts: 100
Joined: Thu Jun 22, 2017 5:54 pm
Location: Russia

HTML and PHP

Fri Aug 14, 2020 6:17 pm

Exist two variables $x=100 and $y=100. How I may insert its to div tag
<div style="position:absolute; left:100; top:100;">
to get something like this:
<div style="position:absolute; left:$x; top:$y;">
Thanks.

Heater
Posts: 16092
Joined: Tue Jul 17, 2012 3:02 pm

Re: HTML and PHP

Fri Aug 14, 2020 6:57 pm

If it were me I'd probably do it the way the PHP documention says: https://www.php.net/manual/en/faq.html.php

Or the million PHP tutorials one finds on the net. For example: https://code-boxx.com/display-php-variables-in-html/
Memory in C++ is a leaky abstraction .

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: HTML and PHP

Fri Aug 14, 2020 8:35 pm

I'd probably build the whole <DIV> tag with a php echo/print command.

Code: Select all

<?php
$x = 100;
$y = 100;
echo "<div style=\"position:absolute; left:".$x."; top:".$y.";\">";
?>
You can do it with

Code: Select all

<?php $x = 100;
$y = 100;?>
<div style="position:absolute; left:<?php echo $x;?>; top:<?php echo $y;?>;">
I don't know which I prefer, both are equally ugly.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Heater
Posts: 16092
Joined: Tue Jul 17, 2012 3:02 pm

Re: HTML and PHP

Fri Aug 14, 2020 9:18 pm

DougieLawson wrote:
Fri Aug 14, 2020 8:35 pm
I don't know which I prefer, both are equally ugly.
It's not often that we agree. But yes, how have people tolerated this mess for so long?
Memory in C++ is a leaky abstraction .

trejan
Posts: 2234
Joined: Tue Jul 02, 2019 2:28 pm

Re: HTML and PHP

Fri Aug 14, 2020 9:26 pm

DougieLawson wrote:
Fri Aug 14, 2020 8:35 pm
I'd probably build the whole <DIV> tag with a php echo/print command.

Code: Select all

<?php
$x = 100;
$y = 100;
echo "<div style=\"position:absolute; left:".$x."; top:".$y.";\">";
?>
You don't need to concatenate the string and variables. The PHP interpreter will parse double quoted strings and expand variables.

Code: Select all

<?php
$x = 100;
$y = 100;
echo "<div style=\"position:absolute; left:$x; top:$y;\">";
?>

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: HTML and PHP

Fri Aug 14, 2020 9:51 pm

I prefer to use the concatenation because then vi's colouring makes it clear there's a variable embedded in there.

Talking about PHP always reminds me of this https://eev.ee/blog/2012/04/09/php-a-fr ... ad-design/ (published on my 49th birthday). There's some absolute gems in the comments on that page.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Heater
Posts: 16092
Joined: Tue Jul 17, 2012 3:02 pm

Re: HTML and PHP

Fri Aug 14, 2020 10:26 pm

Wow, that was written in 2012.

I was desperate enough to take a job working with PHP for a year around about 2000. Not a "fly by night", but Finland's biggest news/media company at the time.

I was totally shocked to find that this is what people were building the web with. They want to base serious commerce on this ill specified, inconsistent pile of cra...

I promised myself I would rather starve in the streets rather that stoop to that ever again.
Memory in C++ is a leaky abstraction .

pidd
Posts: 721
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: HTML and PHP

Sat Aug 15, 2020 12:29 am

There is the php short echo tag which makes it look slightly tidier but less readable

Code: Select all

<div style="position:absolute; left:<?= $x ?>; top:<?= $y ?>;">

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: HTML and PHP

Sat Aug 15, 2020 10:41 am

pidd wrote:
Sat Aug 15, 2020 12:29 am
There is the <insert PHP function name here> which makes it look slightly tidier but less readable
That exactly explains everything that's wrong with PHP in a single sentence.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Heater
Posts: 16092
Joined: Tue Jul 17, 2012 3:02 pm

Re: HTML and PHP

Sat Aug 15, 2020 12:07 pm

A web page is a wonderful thing. How many different languages can one squeeze into a single source file?

HTML of course,
Javascript,
CSS,
PHP,
SQL,

Likely more I have forgotten. All separated by some wonky schemes of escape sequences.

No wonder everything created with it, i.e. the whole web, is so error prone and insecure.
Memory in C++ is a leaky abstraction .

pidd
Posts: 721
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: HTML and PHP

Sat Aug 15, 2020 5:50 pm

DougieLawson wrote:
Sat Aug 15, 2020 10:41 am
pidd wrote:
Sat Aug 15, 2020 12:29 am
There is the <insert PHP function name here> which makes it look slightly tidier but less readable
That exactly explains everything that's wrong with PHP in a single sentence.

Yes and no. There are many ways to skin a cat in most languages and systems, the best way is the one that works for you. PHP does it for me and I like the way it developed as a general programming language not just an html feed (because it is not driven by the browser companies).

What's your weapon of choice? - out of curiosity, not to pull it apart.

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: HTML and PHP

Sat Aug 15, 2020 7:05 pm

This week I completed a 1491 line (not including copybooks some of which were generated with DCLGEN, some of which were hand coded) COBOL/SQL program. I was also doing battle with a COBOL DL/I program (until I worked out the hierarchical keys I needed) using the output from the Db2 SQL program. Part of the fix for the DL/I program was changing the output from the SQL program.

On Tuesday, I wrote six simple DL/I programs; two in S/390 assembler, two in COBOL and two in PL/I.

I've also been working with lots of Easytrieve (EZT) programs for a different client in the last three weeks.

I was tinkering with a bit of Javascript that calls a CGI program written in C with sqlite's SQL dialect yesterday.

I've used PHP in the past, because it's ugly but can get a simple job done very easily, probably with excessively ugly code.


I have no preference, for my day job I'll code in whatever language pays a daily rate.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Heater
Posts: 16092
Joined: Tue Jul 17, 2012 3:02 pm

Re: HTML and PHP

Sat Aug 15, 2020 9:33 pm

pidd wrote:
Sat Aug 15, 2020 5:50 pm
What's your weapon of choice? - out of curiosity, not to pull it apart.
After decades of programming in a dozen or more languages, a lot of which are now not only obsolete but unheard of, I have come down to two weapons of choice:

Rust - After a long time using C and C++ it's great to find a language that can do all that without all the footguns. Now we have Rust in our servers, serving up web pages and web sockets, juggling them with NATS messaging and databases. We have Rust in our remote embedded devices collecting data from sensors and such, some of which are Raspberry Pi. Rust is one of the few languages I have ever found that offers genuinely new features that not only incredibly useful but, practically useful. Especially when performance is required. All the type checking and data reference anti-aliasing means you can be very confident that what you build will not crash and burn with hard to find data corruptions.

Javascript - The much maligned JS turns out to be brilliant. It has always had features that other languages are only now starting to to adopt, first class functions, closures, asynchronous programming, etc. And modern JS engines do all that with very good performance unlike many interpreted languages. With JS running under node.js one can do all that web server stuff without all that complexity and chaos of Apache and PHP or whatever. And a lot more. JS is dynamically typed so it's very quick and easy to get a prototype idea working. And allows one to do things that are not even expressible in many other languages.

This is perhaps paradoxical, I love Rust because it is so extremely strict about types and data ownership. I love JS because it so extremely isn't! :)

Actually I did not understand the comment about " I like the way it developed as a general programming language not just an html feed (because it is not driven by the browser companies)."

Firstly I cannot imagine anyone using PHP as a general programming language. There are so many better options. And PHP is a "Fractal of bad Design": https://eev.ee/blog/2012/04/09/php-a-fr ... ad-design/. Even if one can put up with that what about performance?

Secondly, it turns out that Rust originates from a browser company, Mozilla, but is for sure intended as a general purpose language. Javascript was of course created by Netscape, now Mozilla, but is also a perfectly good general purpose language outside of the browser. See node.js, Espruino etc.
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”