php in image alt tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: php in image alt tag

Post by Christopher »

You can use PHP to display the value of a variable anywhere in a PHP page. There are several different variations to do this:

Code: Select all

// show just the $title variable
<img src="images/logo/logo.png" alt="<?php echo $title; ?>" title="">
// OR show the whole tag with single quotes by concatenating
<?php echo '<img src="images/logo/logo.png" alt="' . $title . '" title="">'; ?>
// OR show the whole tag and escape double quotes
<?php echo "<img src=\"images/logo/logo.png\" alt=\"$title\" title=\"\">";  ?>
(#10850)
kiash
Forum Newbie
Posts: 7
Joined: Sat Aug 07, 2021 1:42 am
Contact:

Re: php in image alt tag

Post by kiash »

You can also use printf or spritf function to display alt tags
I am a full stack developer. My persoal site Kiash.io and I work for Slide.pub as a lead developer.
Post Reply