Using php tag in image tag

The img tag is not retrieving image at my page.

<img class="actar" src=".'<'.'?php echo base_url('/image.jpg')?>'.">

But think it is correct code. Image location is a problem. I want to use base_url() in img tag and class too.

Give your suggestions

3 answers

  • answered 2018-05-16 05:29 SINGH

    Try to using below code

    <img class="actar" src="<?php echo base_url('/image.jpg');?>">
    

  • answered 2018-05-16 05:31 Abdulla Nilam

    Use this

    <img class="actar" src="<?php echo base_url('image.jpg'); ?>">
    

    Most of the time base url set with / at end https://stackoverflow.com/ so you'll get https://stackoverflow.com//image.jpg if you used /image.jpg. so remove / from img tag

    And make sure

    1. image.jpg is in your project root
    2. base_url not empty and its set properly

    Instead of adding image.jpg in the root, add it inside assets(new folder) and move. In case if you need, you can prevent URL access to the folder

  • answered 2018-05-16 05:39 pradeep

    It should be like this :

    /*make sure image path is correct */
    
    <img class="actar" src="<?=base_url('image.jpg');?>">
    

    You can also use img from the CI html helper like this (Alternative way)

    In autoload.php load html helper

    $autoload['helper'] = array('html');
    

    In your views :

    <?php 
        echo img(['class' => 'actar','src' => 'image.jpg']);
    
        /* Or do like this */
    
        $data = ['class' => 'actar','src' => 'image.jpg'];
        echo img($data);
    ?>
    

    For more : https://www.codeigniter.com/user_guide/helpers/html_helper.html#img